Javascript 在标签上单击“返回其无线组名称”
我想能够点击一个标签,看看它属于哪个单选按钮组。我尝试了这个方法,但它总是在li(name=left)之后找到第一个输入,而不是最接近标签的输入。我该如何解决这个问题Javascript 在标签上单击“返回其无线组名称”,javascript,jquery,Javascript,Jquery,我想能够点击一个标签,看看它属于哪个单选按钮组。我尝试了这个方法,但它总是在li(name=left)之后找到第一个输入,而不是最接近标签的输入。我该如何解决这个问题 $('input[type=radio]:enabled + label').click(function () { var thisGroupName = $(this).closest('ul').find('input[type=radio]:enabled').attr('name'); alert("gr
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $(this).closest('ul').find('input[type=radio]:enabled').attr('name');
alert("group name: " + thisGroupName);
});
HTML
<ul>
<li>
<input type="radio" class="radio-red" name="left" id="a1">
<label for="a1">a1</label>
<input type="radio" class="radio-blue" name="right" id="a2">
<label for="a2">a2</label>
</li>
<li>
<input type="radio" class="radio-red" name="left" id="b1">
<label for="b1">b1</label>
<input type="radio" class="radio-blue" name="right" id="b2">
<label for="b2">b2</label>
</li>
<li>
<input type="radio" class="radio-red" name="left" id="b3">
<label for="b3">b3</label>
<input type="radio" class="radio-blue" name="right" id="b4">
<label for="b4">b4</label>
</li>
</ul>
-
a1
a2
-
b1
b2
-
b3
b4
不要去ul
然后回到li
。如果需要获取输入的组,请尝试使用prev()
:
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $(this).prev('input[type=radio]:checked').attr('name');
alert("group name: " + thisGroupName);
});
为了更具体,您已经准备了for
属性。为什么不用呢
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $("#" + $(this).attr("for")).attr('name');
alert("group name: " + thisGroupName);
});
不要转到ul
然后回到li
。如果需要获取输入的组,请尝试使用prev()
:
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $(this).prev('input[type=radio]:checked').attr('name');
alert("group name: " + thisGroupName);
});
为了更具体,您已经准备了for
属性。为什么不用呢
$('input[type=radio]:enabled + label').click(function () {
var thisGroupName = $("#" + $(this).attr("for")).attr('name');
alert("group name: " + thisGroupName);
});
在您提供的代码示例中,还可以使用单击标签的“for”属性显式查找单选按钮
$(document).ready(function () {
$('input[type=radio]:enabled + label').click(function () {
var forName = $(this).attr('for');
var thisGroupName = $('#' + forName).attr('name');
alert("group name: " + thisGroupName);
});
});
它在这个JSFIDLE中工作:
在您提供的代码示例中,您还可以使用单击标签的“for”属性显式查找单选按钮
$(document).ready(function () {
$('input[type=radio]:enabled + label').click(function () {
var forName = $(this).attr('for');
var thisGroupName = $('#' + forName).attr('name');
alert("group name: " + thisGroupName);
});
});
它在这个JSFIDLE中工作:
我想你需要这个:
$('label').click(function(){
var forRadio = $(this).attr('for');
var groupName = $('#'+forRadio).attr('name');
alert(groupName);
}))
我在这里打了个盹儿
我想你需要这个:
$('label').click(function(){
var forRadio = $(this).attr('for');
var groupName = $('#'+forRadio).attr('name');
alert(groupName);
}))
我在这里打了个盹儿
以这种方式遍历DOM没有意义,因为标签具有对元素的引用。当您单击标签时,它将使用它来单击它
$('input[type=radio]:enabled + label').click(function () {
var input_id = '#'+$(this).attr('for');
var group_name = $(input_id).attr('name');
alert("group name: " + group_name );
});
以这种方式遍历DOM没有意义,因为标签具有对元素的引用。当您单击标签时,它将使用它来单击它
$('input[type=radio]:enabled + label').click(function () {
var input_id = '#'+$(this).attr('for');
var group_name = $(input_id).attr('name');
alert("group name: " + group_name );
});
你想知道标签上的单选按钮的名称吗?@Ahmad,是的。下面的prev()解决了这个问题。@user2011960好的,我想你还有别的想法:)@user2011960你应该接受一个答案,这样这个问题就可以标记为已回答。你想知道标签上的单选按钮的名称吗?@Ahmad,是的。下面的prev()解决了这个问题。@user2011960好的,我想你还有别的想法:)@user2011960你应该接受一个答案,这样这个问题就可以标记为已回答。