javascript - Multiple select form tag for the same name getting the last option always
问题描述
I have been trying to select the table number according to its location. My form is working and the record is added to the database however at the number section, the number that is inserting is "32" no matter what I choose, Even if I choose in the select "tnum3" a different number. I don't know what has happened.
The form
<label for="tnum">table number
<select type="number" id="tnum1" name="num">
<?php for ($i = 2; $i <= 9; $i++) : ?>
<option id="in" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?></select>
<select type="number" id="tnum2" name="num" style="display: none;">
<?php for ($i = 12; $i <= 23; $i++) : ?>
<option id="out" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?></select>
<select type="number" id="tnum3" name="num" style="display: none;">
<?php for ($i = 32; $i <= 35; $i++) : ?>
<option id="tr" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select></label><br>
<label for="loc">location
<select id="loc" name="location" style="width:100%;" onchange="change_tnum(this.value)" required>
<option value="inside" >inside</option>
<option value="outside" >outside</option>
<option value="Porch" >Porch</option>
</select></label><br>
This is the JS function that just toggles the select tags according to the location I choose:
function change_tnum(x){
$('#tnum1').hide();
$('#tnum2').hide();
$('#tnum3').hide();// hide any options already shown
switch (x) { // show whichever option is appropriate
case 'inside':
$('#tnum1').show();
inside();
break;
case 'outside':
$('#tnum2').show();
outside();
break;
case 'porch':
$('#tnum3').show();
terrace();
break;
default:
break;
}
}
解决方案
I don't see any issue with your select, however even if you set the element to display: none
, it's value will still be submitted. Since there are multiple elements of the same name the last value overwrites the previous values.
If you want only one element value then you should add disabled
attribute to it. The elements marked disabled
are not sent to the server. Reference
So, your code changes to -
function change_tnum(x){
$('#tnum1').hide();
$('#tnum1').attr('disabled', true);
$('#tnum2').hide();
$('#tnum2').attr('disabled', true);
$('#tnum3').hide();// hide any options already shown
$('#tnum3').attr('disabled', true);
switch (x) { // show whichever option is appropriate
case 'inside':
$('#tnum1').show();
$('#tnum1').attr('disabled', false); // or simply remove disabled attribute
inside();
break;
case 'outside':
$('#tnum2').show();
$('#tnum2').attr('disabled', false);
outside();
break;
case 'porch':
$('#tnum3').show();
$('#tnum3').attr('disabled', false);
terrace();
break;
default:
// do something
}
}
Hope it helps you.
推荐阅读
- swift - 无法分配类型“[NSAttributedStringKey:Any]”的值来快速键入
- oracle - 如何确保所有事务都已提交或回滚?
- mongodb - 将mongo查询转换为spring查询
- python - tensorflow.gradients 给出 None 值
- macos - 在 OS X 终端中运行时,aiodns 无法联系 DNS 服务器错误
- css - 如何用 css 覆盖一个类?
- javascript - 钩子之前的摩卡没有在套件之前运行
- python - Python:根据变量名称动态命名数据框的自定义函数
- libgdx - 多个屏幕的 Gdx.input.setInputProcessor。(LIBGDX,概念)
- python - 错误: (-215:Assertion failed) s >= 0 in function 'cv::setSize' using LBPHFaceRecognizer