gpt4 book ai didi

javascript - 使用 get 方法在表单中创建随机 url

转载 作者:行者123 更新时间:2023-12-03 02:23:17 25 4
gpt4 key购买 nike

我正在尝试在脚本中创建随机网址,

我想随机生成此网址:

action="https://www.google.com/search?q="

与:

action="https://www.google.com/search?q="
action="https://www.bing.com/search?q="
action="https://search.yahoo.com/search?p="

HTML:

<form id="search-form" target="_blank" method="get" action="https://www.google.com/search?q=">
<input id="aaa" placeholder="..." name="url" type="text">
<input class="bbb" value="search" type="submit">
</form>

谢谢,

最佳答案

function getRandomUrl() {
const urls = [
'https://www.google.com/search?q=',
'https://www.bing.com/search?q=',
'https://search.yahoo.com/search?p='
]
return urls[Math.floor(Math.random() * urls.length)]
}

function randomize() {
const url = getRandomUrl()
const $form = document.getElementById('search-form')
$form.setAttribute('action', url)
}

randomize()
<form id="search-form" target="_blank" method="get" action="https://www.google.com/search">
<input id="aaa" placeholder="..." name="url" type="text">
<input class="bbb" value="search" type="submit">
</form>

<button onclick="randomize()">random</button>

关于javascript - 使用 get 方法在表单中创建随机 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49062798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com