选定的按钮值,当分配给变量时返回undefined

时间:2014-06-26 18:43:10

标签: jquery

这将返回所选值并正常工作:

    $('document').ready(function () {
        $(document).on('click', '#square', function () {
            confirm($('input[type="radio"][name="plot"]:checked').val());
        });
    });

返回undefined:

     $('document').ready(function () {
         var $userInput;
         $userInput = $('input[type="radio"][name="plot"]:checked').val();
         $(document).on('click', '#square', function () {
             confirm($userInput);
         });
     });

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

在第一个场景中,您在点击时获取值,在第二个场景中,您将获得页面加载时的值。

解决方案:抓住事件处理程序中的值,如第一个示例所示。