【问题标题】:jQuery .change() on Radio Button单选按钮上的 jQuery .change()
【发布时间】:2011-02-08 07:43:57
【问题描述】:

我必须在这里遗漏一些明显的东西......我不能让.change() 触发单选按钮?我有下面的代码直播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>

谁能看到我错过了什么?

谢谢, 丹尼斯

【问题讨论】:

标签: javascript jquery input


【解决方案1】:

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

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

否则脚本会在 HTML 元素加载之前执行。因此,不存在单选框。

【讨论】:

  • 非常感谢您发布此内容!
【解决方案2】:

你的

$("output").text("a changed");

也应该是

$("#output").text("a changed");

因为它是您要匹配的 id。

【讨论】:

    猜你喜欢
    • 2012-08-26
    • 2012-03-22
    • 1970-01-01
    • 2012-06-28
    • 2018-12-18
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多