Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何获取单选按钮组选择_Jquery - Fatal编程技术网

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
});