简体   繁体   English

jQuery-使用收音机的值填充最接近的隐藏输入

[英]jQuery - Populate closest hidden input with radio's value

I have the following HTML and jQuery to populate hidden fields within each <td> element: 我有以下HTML和jQuery来填充每个<td>元素内的隐藏字段:

<td>
    <div id="sex_div" style="text-align: left; width:120;">
        <input type="hidden" id="sex" value=""/>
        <input type="radio" id="sex_male" name="sex_div" value="M"/><label for="sex_male">Male</label>
        <input type="radio" id="sex_female" name="sex_div" value="F"/><label for="sex_female">Female</label>
    </div>
</td>

The jQuery I have is as follows: 我拥有的jQuery如下:

$("input :radio").click(function() {
    $(this).siblings("input [type='hidden']").val($(this).val());
});

and obviously the buttonset, 显然是按钮集

$("#sex_div").buttonset();

This is just a small part of the whole form. 这只是整个表格的一小部分。 The rest all looks similar. 其余的看起来都差不多。 Now the issue is that the hidden field is not being set when clicking/selecting a radio button. 现在的问题是,单击/选择单选按钮时未设置隐藏字段。 I have been struggling with this seemingly easy problem for two days now! 我已经为这个看似简单的问题苦苦挣扎了两天了!

Thanks for any help! 谢谢你的帮助!

$(this).siblings("input[type='hidden']").val($(this).val());

There should be no space between input and [type='hidden'] . input[type='hidden']之间不应有空格。 Spaces in selectors mean to search for descendant elements that match the next token. 选择器中的空格表示搜索与下一个标记匹配的子代元素。

you need to remove space in your selector 您需要删除选择器中的空间

$("input:radio").click(function() {
    $(this).siblings("input[type='hidden']").val($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM