gpt4 book ai didi

javascript - 如何从选择列表中获取值?

转载 作者:行者123 更新时间:2023-12-03 11:49:46 26 4
gpt4 key购买 nike

我试图从选择列表中获取一个值,该值将确定我的表单方法是什么,是 get 还是 post。不过我在做这件事时遇到了麻烦。有什么建议吗?

<select name ="optionlist" form ="form" id="test">
<option value="get">GET</option>
<option value="post">POST</option>
</select>

<form name="input" action="" method="" id="form">
<input type="text" name="search">
<input name="buttonExecute" type="submit" value="Submit"/>
</form>

JS

function myFunction(input){
alert("You typed in : " + input);
}

function load(){
alert("Page Loaded! This is the current text in the textbox : "+ document.getElementsByName('search')[0].value);
}

func

window.onload = load;

任何帮助将不胜感激!谢谢!

最佳答案

首先,向您的选择元素添加一个事件监听器,以便在其值更改时触发一个函数:

document.getElementById('test').addEventListener('change', setFormMethod, true);

其次,确保触发的函数更改表单的方法属性:

function setFormMethod(e) {
document.getElementById('form').setAttribute('method', e.target.value);
}

编辑

<html>
<head>
</head>
<body>
<!-- your HTML here -->
<script>
// my code here
</script>
</body>

关于javascript - 如何从选择列表中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25899113/

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