使用 jQuery 更改隐藏字段的值只会“触发”适用于第一个更改,但不适用于以下更改
我对 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());
});
});
当我加载页面并单击“模型发布”时,它会将 #mr 设置为 1,取消选择它会将其设置为 0。但是对于其他复选框或单选按钮,什么也不会发生。然而,如果我再次加载页面并单击“属性发布”,它会将 #pr 设置为 1,但它不再对其他元素执行任何操作。对于单选按钮也是如此:我加载页面并单击它们,它们可以工作,但所有其他按钮都停止工作。
我希望我遗漏了一些非常基本的东西(或者更确切地说:我担心情况可能如此,而我太愚蠢了,看不到它......) 另外,代码可能不是最聪明的,所以如果有人有任何改进建议,我会很高兴看到它们。
干杯, 吉贝
I have a strange phenomenon with jQuery. I have a div with a few checkboxes and radio buttons
<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>
When selecting the checkboxes or radiobuttons I want to change hidden fields that are part of the "real" form that is elsewhere in the markup.
<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>
I thought that the following jQuery code would do it
$(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());
});
});
When I load the page and click on "Model Release" it sets #mr to 1, deselecting it sets it 0. But with the other checkbox or the radiobuttons nothing happens. If I however load the page again and click on "Property Release" it sets #pr to 1 but it doesn't do anything for the other elements anymore. And the same for the radiobuttons: I load the page and click on them, they work but all others stop working.
I hope that there is something very basic that I'm missing (or rather: I'm afraid that that might be the case and I'm too stupid to see it ...)
Also, the code might not be the smartest, so if anyone has any improvement suggestions, I'll be happy to see them.
cheers,
jibe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论