1

我有一组同名的单选按钮。我使用选择器按.changed名称将事件附加到单选按钮组。事件触发,但.val()单选按钮组的值是更改前组的值,而不是更改后的值。更改后如何获得价值,一定是我做错了什么。

$('[name="Messages"]').click(function () { 
    someFunction('Messages', $('[name="Messages"]').val()) 
});
4

2 回答 2

4

You probably want the value for the particular radio button that was clicked on. You can use $(this).val() to get it. Otherwise you need to clarify, there's no such thing as a "group value". Each individual radio button should have it's own value.

$('[name="Messages"]').click(function () { 
    someFunction('Messages', $(this).val());  
});
于 2012-05-04T16:58:50.080 回答
1

您没有过滤掉未选中的单选按钮,因此您的点击将始终带回 DOM 中第一个单选按钮的值。尝试过滤掉:checked

$('[name="Messages"]').click(function () { 
    someFunction('Messages',$('[name="Messages"]:checked').val());
});

小提琴:http: //jsfiddle.net/T7Q8T/

于 2012-05-04T17:01:51.427 回答