7

我可以value通过.click(function()在页面加载时选择)。

我认为这样的事情可能会起作用,但它不会:

$("input[type=radio][name=q22]:checked").val()

有什么建议么?

4

6 回答 6

19

Try with:

$(document).ready(function(){
    $("input[type=radio][name='q22']:checked").val()
});
于 2013-09-07T07:19:57.957 回答
5

Your code is ok.

What you might have forgoten is to run it after page has loaded and the html has been read. You can use jQuery's .ready() to do this.

$(document).ready(function () {
    var i = $("input[type=radio][name=q22]:checked").val();
    console.log(i);
});

Demo here

于 2013-09-07T07:22:32.187 回答
1

要么把你的脚本放在页面底部,就在结束</body>标记之前,要么使用 jQuery 的readyevent。在任何一种情况下,脚本都可以访问页面上的元素,并且可以获得选中的单选按钮的值。

无偿的活生生的例子| 资源

于 2013-09-07T07:19:05.683 回答
1

used $(document).ready(handler)

Specify a function to execute when the DOM is fully loaded.

Check This

$(document).ready(function() {
    // Handler for .ready() called.
    // Code run after DOM load success 
});


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  alert("")
});
</script>
</head>
<body>
</body>
</html>

it will alert after BODY loaded success

于 2013-09-07T07:21:28.277 回答
1

尝试

var arr=$("input[type=radio][name=q22]:checked").map(function(){return $(this).val();})
于 2013-09-07T07:23:36.097 回答
0

尝试这个 :

$(function(){
    $("input[type=radio][name='q22']:checked").val();
});

否则你也可以使用这个:

    $(function(){
       if ($("#radio1").is(":checked")) {
         // do something
       }
    });
于 2013-09-07T07:18:57.163 回答