jquery radio/checkbox change事件不能触发的问题

news/2024/7/11 2:00:42 标签: jquery, javaScript

我想让radio来控制当前我选择的是机动车还是特种车,如下所示: 

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
</head> 
<body> 
    <input type="radio" id="insuranceType" name="insuranceType" value="0"/>机动车 
    <input type="radio" id="insuranceType" name="insuranceType" value="1"/>特种车 
    <script type="text/javascript"> 
        $(document).ready(function () 
        { 
            $("#insuranceType").change(function () 
            { 
                    var insuranceTypeVar = $("input[name='insuranceType']:checked").val(); 
                alert(insuranceTypeVar); 
            }); 
        }); 
    </script> 
</body> 
</html> 

 
可结果今我很困惑,只有前一个radio选择时才触发change事件,后才发现因为我是使用的 id 作为筛选器!重复id只能选中第一个! 
于是加上一个div,如下所示,一切OK 

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
</head> 
<body> 
    <div id="insuranceTypeSelect"> 
    <input type="radio" name="insuranceType" value="0"/>机动车 
    <input type="radio" name="insuranceType" value="1"/>特种车 
  </div> 
    <script type="text/javascript"> 
        $(document).ready(function () 
        { 
            $("#insuranceTypeSelect").change(function () 
            { 
                    var insuranceTypeVar = $("input[name='insuranceType']:checked").val(); 
                alert(insuranceTypeVar); 
            }); 
        }); 
    </script> 
</body> 
</html> 

 
        以上还可以优化下,不仅是单击radio圆圈的时候可以选中,单击后边的机动车(特种车)文字的时候,也可以实现radio的选中并触发change事件,如下所示: 

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 
</head> 
<body> 
    <div id="insuranceTypeSelect"> 
    <input type="radio" id="insuranceType1" name="insuranceType" value="0"/><label for="insuranceType1">机动车</label> 
    <input type="radio" id="insuranceType2" name="insuranceType" value="1"/><label for="insuranceType2">特种车</label> 
  </div> 
    <script type="text/javascript"> 
        $(document).ready(function () 
        { 
            $("#insuranceTypeSelect").change(function () 
            { 
                    var insuranceTypeVar = $("input[name='insuranceType']:checked").val(); 
                alert(insuranceTypeVar); 
            }); 
        }); 
    </script> 
</body> 
</html>

 


http://www.niftyadmin.cn/n/1319262.html

相关文章

WPF选择.NET Framework 4 Client Profile提示找不到名字空间

在应用程序的Target framework: .NET Framework 4 与 .NET Framework 4 Client Profile 当程序集目标框架为.NET Framework 4 Client Profile时&#xff0c;引用的其他程序集&#xff0c;在代码中的调用一直提示找不到名字空间 当选择.NET Framework 4时就好了 可恶&#xff0c…

react native app应用更新方案

最近公司有需求做一个react native app应用更新方案,试了很多插件都不靠谱 最后还是选择了 搭配 rn-fetch-blob 进行封装 引入 rn-fetch-blob import RNFetchBlob from rn-fetch-blob 需要从后台获取最新版本号 接下来就是点击更新的代码分享 // 版本更新update () {let …

JS的undefined与null的实例

<form name"theform" id"theform"> </form><script language"javascript">var aalert(typeof(b)); //这里提示undefinedif(theform.datasourcenull)alert("null")else if(theform.datasourceundefined)alert(&qu…

OSPF链路状态更新

链路状态更新 (LSU) 数据包用于 OSPF 路由更新。一个 LSU 数据包可能包含11类型的链路状态通告 (LSA)&#xff0c;术语“链路状态更新 (LSU)”和“链路状态通告 (LSA)”之间的差异有时较难分清。有时&#xff0c;它们可以互换使用。一个 LSU 包含一个或多个 LSA&#xff0c;这两…

写一个react native自定义的版本升级弹框

效果&#xff1a; 下面上代码 class VersionModal extends React.Component {constructor(props) {super(props)this.state {isShow: false, //在父组件控制显示隐藏imgObjHg: 0 // 根据宽度拿到图片自适应高度}}componentDidMount() {const { width: screenWidth } Dimens…

JS精度问题

在JS处理算法时&#xff0c;有时会存在精度问题。 如694.84(-300)有精度问题&#xff0c;但694.64(-300)没有精度问题。 如0.263*256.3有精度问题&#xff0c;但0.263*256.5没有精度问题。 但在实际应用中&#xff0c;是绝对不能出现此类问题的。如下提供了一种解决此类精度问题…

fckeditor 2.6X 0day

我讲一下。这个可以说是fckeditor 2.63 oday 。目前知道的人比较多了。也比较老了 http://xxxx.com/fckeditor/editor/filemanager/connectors/test.html http://xxxx.com/fckeditor/editor/filemanager/connectors/uploadtest.html 这个两个页面&#xff0c;是死的关键。上传x…

javascript闭包[两个小测试例子]

一.程序一 <script> var name "The Window"; var Object_a {name : "My Object",getNameFunc : function(){var that this;return function(){return that.name;};} }; alert(Object_a.getNameFunc()()); </script> 结果&#xff1a;My Obj…