我正在使用JQuery根据另一个选择下拉列表的值更新选择下拉列表的defualt选项。由于某种原因,该更新仅适用于第一个更改,我无法弄清原因。
HTML:
<label>Enclosure Type:</label>
<select name="enclosure_type_it">
<option value="global">Global </option>
<option value="server_optimized">Server Optimized </option>
</select>
<div class="pure-control-group">
<label>Handles:</label>
<select name="handles_it" id="handles_datacenter">
<option value="Semi Cylinder">Semi Cylinder</option>
<option value="Combo_Security Handle">Combo/Security Handle </option>
<option value="Comfort Handle">Comfort Handle </option>
</select>
</div>
<div class="pure-control-group">
<label>Inserts:</label>
<select name="inserts_it" id="inserts_datacenter">
<option value="Pushbutton">Pushbutton </option>
<option value="Locking pushbutton">Locking pushbutton </option>
<option value="Locking">Locking </option>
</select>
</div>
jQuery的:
$('#enclosure_type_it').change(function() {
if ($(this).val() == "global") {
$('#handles_datacenter option[value="Semi Cylinder"]').attr("selected", true);
$('#inserts_datacenter option[value="Locking"]').attr("selected", true);
}
if ($(this).val() == "server_optimized") {
$('#handles_datacenter option[value="Comfort Handle"]').attr("selected", true);
$('#inserts_datacenter option[value="Locking pushbutton"]').attr("selected", true);
}
}).change();
仍然不确定我在做什么错:http : //jsfiddle.net/y7easnb7/
使用.val()
到设定值也需要使用enclosure_type_it
作为选择ID。
的HTML
<select id="enclosure_type_it">
脚本
$('#enclosure_type_it').change(function() {
if ($(this).val() == "global") {
$('#handles_datacenter').val("Semi Cylinder");
$('#inserts_datacenter').val("Locking");
}
if ($(this).val() == "server_optimized") {
$('#handles_datacenter').val("Comfort Handle");
$('#inserts_datacenter').val("Locking pushbutton");
}
}).change();
如果要使用名称属性。使用以下内容作为选择器
$('select[name="enclosure_type_it"]')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句