Jquery 如何获取单选按钮组选择
我有下面的代码,尽管有一种更优雅的方法来获取所选的单选值,因为我已经在单选按钮组的代码中了。这是为了获取组中选定的单选按钮。 非常感谢,Jquery 如何获取单选按钮组选择,jquery,Jquery,我有下面的代码,尽管有一种更优雅的方法来获取所选的单选值,因为我已经在单选按钮组的代码中了。这是为了获取组中选定的单选按钮。 非常感谢, $('input[name=pig]').change(function () { // this seems redundant namespace('bla').myVariable = $('input[name=pig]:checked').val(); // ie. something like namespa
$('input[name=pig]').change(function () {
// this seems redundant
namespace('bla').myVariable = $('input[name=pig]:checked').val();
// ie. something like
namespace('bla').myVariable = $(this).checked.val();
});
我认为这就是你想要实现的目标:
namespace('bla').myVariable = this.value;
这应该可以做到
namespace('bla').myVariable = $(this).val();
$(this).val()
就足够了:
$('input[name=pig]').change(function () {
console.log($(this).val()); // or this.value
});