我有一个由选择列表和输入字段组成的表单。如果输入框有值,我希望覆盖选择列表值,无论选择什么。
看起来像是
myMap={"asdf":1,"qwerty":2}
function checkInput(){
desiredSel=$('#input_id').val();
if(desiredSel){
alert(desiredSel);
if(myMap[desiredSel]){
alert(myMap[desiredSel]);
//This is where I'm lost
}
}
}
<form onSubmit=checkInput()>
<select name='myselect' id='select_id'>
<option value=1>First</option>
<option value=2>second</option>
</select>
<input name='myinput' id='input_id' />
</form>
在我的评论中,我不清楚如何覆盖为$(#select_id)发送的值
编辑澄清:
如果myinput为空,那么myselect将被提交,其中包含实际选择的选项
如果myinput有一个值并且它匹配myMap中的一个键,则myselect将与myMap [myinput]值一起提交,就好像它实际上是在该选项上被选中一样(无论实际选择了什么)。
答案 0 :(得分:1)
应该是你在找什么
$('#select_id').val(myMap[desiredSel]);
.val()可以返回或设置一个值,但在https://jquery.com/
中指定执行验证功能后的更多信息,您可以更改所有值并按return true;
提交,或按return false;