我有 HTML:
<select class="form" id="select" name="select">
<option>number1</option>
<option>number2</option>
</select>
<select name="organisation" class="form" id="cent" required>
<option value="1">UK</option>
<option value="2">SPAIN</option>
</select>
还有一个<select>
:
<select class="form" id="ssip" name="organisation">
<option value="47" >US</option>
<option value="48">UKRAINE</option>
</select>
JQuery
显示在 1 - 首先选择 2 - 第二
$(document).ready(function() {
$("#select").each(function () {
if (this.value == "1") {
$('#cent').show();
$('#ssip').hide();
}
if (this.value == "2") {
$('#cent').hide();
$('#ssip').show();
}
});
});
问题是,当我选择选项 number1 时,首先选择值 1 和 2,但是当我选择 UK 值 1 并单击提交时,它发送值 47,第二个选择的第一个选项..
最佳答案
在切换 select
时尝试禁用相反的选择。如果将显示设置为 none
,表单上的控件仍将被提交,但是如果控件被禁用,它将不会与表单一起提交。
$(document).ready(function() {
$("select").change(function () {
if (this.value == "number1") {
$('#cent').show().prop("disabled", false);
$('#ssip').hide().prop("disabled", true);
}
if (this.value == "number2") {
$('#cent').hide().prop("disabled", true);
$('#ssip').show().prop("disabled", false);
}
});
});
JS fiddle : http://jsfiddle.net/bTJsH/
关于javascript - 选择不正确的值 HTML <select>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288781/