我试图通过谷歌找到我的问题的答案,但收效甚微.我是jQuery的新手,所以答案可能很简单.
我的表单上有2个下拉字段,都是从数据库中填充的.
<select name="fld_1" id="fld_1">
..set of options from DB..
</select>
<select disabled name="fld_2" id="fld_2">
Please select value from field above
</select>
Run Code Online (Sandbox Code Playgroud)
第二个字段被禁用,直到用户从第一个字段中选择一个值.该值传递给运行数据库检查的php文件,并返回一组选项.所有这些都是由jQuery控制的:
$(function() {
$('#fld_1').change(function() {
$('#fld_2').load('fetch.php?ccID=' + $('#fld_1').val());
});
});
Run Code Online (Sandbox Code Playgroud)
如果找到结果,则输出PHP:
<option value="aaa">
aaa - descr
</option>
<option value="bbb">
bbb - descr
</option> ....
Run Code Online (Sandbox Code Playgroud)
PHP输出没有找到结果:
<option value="0">
No accounts found for this cost center
</option>
Run Code Online (Sandbox Code Playgroud)
我想要达到的目的是:
if the value of the first option is 0 keep the dropdown disabled, if anything else remove the disable attribute.
谢谢你的帮助
我建议:
$('#fld_1').change(function(){
$('#fld_2').prop('disabled', $(this).val() === 0);
}).change();
Run Code Online (Sandbox Code Playgroud)
参考文献: