gpt4 book ai didi

javascript - 如何使用 jquery 获取 div 中当前选定的单选按钮的值?

转载 作者:行者123 更新时间:2023-12-03 11:47:45 24 4
gpt4 key购买 nike

我有这个标记:

<div id="filter-1" class="form-radios">

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-all" name="field_filter_1_value" value="All" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-1" name="field_filter_1_value" value="1" checked="checked" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-2" name="field_filter_1_value" value="2" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-3" name="field_filter_1_value" value="3" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-4" name="field_filter_1_value" value="4" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" name="field_filter_1_value" value="5" class="form-radio">
</div>

<div class="form-item form-radio form-item-field-filter-1-value">
<input type="radio" id="filter-1-value-6" name="field_filter_1_value" value="6" class="form-radio">
</div>

</div>

<div id="filter-2" class="form-radios">

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-all" name="field_filter_1_value" value="All" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-1" name="field_filter_1_value" value="1" checked="checked" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-2" name="field_filter_1_value" value="2" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-3" name="field_filter_1_value" value="3" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-4" name="field_filter_1_value" value="4" class="form-radio">

</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" name="field_filter_1_value" value="5" class="form-radio">
</div>

<div class="form-item form-radio form-item-field-filter-2-value">
<input type="radio" id="filter-2-value-6" name="field_filter_1_value" value="6" class="form-radio">
</div>

</div>

我正在尝试从包含在主 div 内的单选按钮组中获取选定的值。

我的想法是检查所有包含单选按钮的主 div,然后获取所选单选按钮的值,但我的代码似乎不起作用。

这是我的 JQuery:

		$(".form-radios").each(function(){

var radio_val = $(this).closest('input[type="radio"]:checked').val();

console.log(radio_val);

});

最佳答案

您不应该使用closest()方法,因为它将在 DOM 上向上移动,而不是向下移动。您需要使用find()方法举例:

$(this).find('input[type="radio"]:checked').val();

关于javascript - 如何使用 jquery 获取 div 中当前选定的单选按钮的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com