单选按钮上的jQuery .change() [英] jQuery .change() on Radio Button

查看:65
本文介绍了单选按钮上的jQuery .change()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定在这里缺少明显的东西...我无法得到 .change()在单选按钮上触发?我在此处

I must be missing something obvious here... I can't get .change() to fire on radio buttons? I have the code below live here!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Radio Button jQuery Change</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        console.log("parsed");
        $("input[name='rdio']").change(function() {
            console.log("changed");
            if ($("input[name='rdio']:checked").val() == 'a')
                $("output").text("a changed");
            else if ($("input[name='rdio']:checked").val() == 'b')
                $("output").text("b changed");
            else
                $("output").text("c changed");
        });
    </script>
</head>
<body>
    <div>
        <input type="radio" name="rdio" value="a" checked="checked" /> a <br/>
        <input type="radio" name="rdio" value="b" /> b <br/>
        <input type="radio" name="rdio" value="c" /> c
    </div>
    <h3>Output:</h3>
    <div id="output"></div>
</body>
</html>

任何人都可以看到我错过的内容吗?

Can anyone see what I've missed?

谢谢, 丹尼斯

推荐答案

您必须将代码放入dom-ready事件中...

You must put the code inside the dom-ready event...

 $(document).ready(function(){
   // Your code here
 });

,否则脚本将在加载HTML元素之前执行.因此,不存在任何单选框.

or else the script gets executed before the HTML-elements have been loaded. Thus, no radioboxes exist.

这篇关于单选按钮上的jQuery .change()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆