我对jQuery有一个奇怪的现象。我有一个带有几个复选框和单选按钮的div
<div id="advanced_search">
<div>
<div class="searchoptions">
<input type="checkbox" id="hidden_mr" name="hidden_mr" />
<label for="hidden_mr">Model Release</label>
</div>
<div class="searchoptions">
<input type="checkbox" id="hidden_pr" name="hidden_pr" />
<label for="hidden_pr">Property Release</label>
</div>
</div>
<div>
<div class="searchoptions">
<input type="radio" id="hidden_portrait" name="hidden_format" value="portrait" />
<label for="hidden_portrait">Portrait</label>
</div>
<div class="searchoptions">
<input type="radio" id="hidden_landscape" name="hidden_format" value="landscape" />
<label for="hidden_landscape">Lanscape</label>
</div>
<div class="searchoptions">
<input type="radio" id="hidden_square" name="hidden_format" value="square" />
<label for="hidden_square">Square</label>
</div>
<div class="searchoptions">
<input type="radio" id="hidden_pano" name="hidden_format" value="pano" />
<label for="hidden_pano">Panoramic</label>
</div>
</div>
</div>
当选择复选框或单选按钮时,我想更改隐藏字段,这些字段是标记中其他位置的“真实”表单的一部分。
<form method="get" name="searchbox" id="searchbox" action="">
<input type="hidden" id="mr" name="mr" value="" />
<input type="hidden" id="pr" name="pr" value="" />
<input type="hidden" id="format" value="" />
</form>
我认为以下jQuery代码会这样做
$(document).ready(function() {
$('#hidden_mr').change(function() {
if ($('#hidden_mr').attr('checked')) { $('#mr').val(1); }
else { $('#mr').val(0); }
});
$('#hidden_pr').change(function() {
if ($('#hidden_pr').attr('checked')) { $('#pr').val(1); }
else { $('#pr').val(0); }
});
$('input:radio[name=hidden_format]').change(function() {
$('#format').val($('input:radio[name=hidden_format]:checked').val());
});
});
当我加载页面并单击“Model Release”时,它将#mr设置为1,取消选择它将其设置为0.但是使用另一个复选框或radiobuttons没有任何反应。但是,如果我再次加载页面并单击“Property Release”,它会将#pr设置为1,但它不再对其他元素执行任何操作。对于radiobuttons也一样:我加载页面并点击它们,它们可以正常工作,但其他所有页面都停止工作。
我希望我缺少一些非常基本的东西(或者更确切地说:我担心情况可能如此,我看起来太傻了......) 此外,代码可能不是最聪明的,所以如果有人有任何改进建议,我会很高兴看到它们。
欢呼声, 嘲笑