zoukankan      html  css  js  c++  java
  • jquery radio 取值

    网上流行的说法就是

    $(input[name='aaa'][checked]).val()

    能取到选中项的value,但我测试后发现只在IE下有效,在firefox和Chrome中不论选中哪一项,或者不选,取到的值都是第一项的value
    正确做法应该是

    $("input[name='aaa']:checked").val()

    同样对于checkbox也是这种写法

     <html>
    <head>
     
    <script type="text/javascript" src="ui/jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
    $(function(){
    	$('#go').click(function(){
    		var radio = $('input[name="testradio"]').filter(':checked');
    		if(radio.length)
    		alert(radio.val());
    		else
    		alert('请选择一个radio');
    		
    	});
    	$('#go2').click(function(){
    		$('input[name="testradio"]').each(function(){
    			alert(this.value);
    		});
    	})
    	$('#go3').click(function(){
    		alert($('input[name="testradio"]:eq(1)').val());
    	})
    	
    	$('#go4').click(function(){
    		 
    		 alert($("input[name='testradio']:checked").val());
    		
    	});
    })
    </script>
    
    </head>
    
    <body>
    <input type="radio" name="testradio" value="radio1" 
    />jquery获取radio的值<br /> <input type="radio" name="testradio"
     value="radio2" />jquery获取checkbox的值<br /> 
    <input type="radio" name="testradio" value="radio3" 
    />jquery获取select的值<br />
    <button id="go">选中的那个radio的值</button>
    <button id="go2">遍历所有radio的值</button>
    <button id="go3">取第二个radio的值</button>
    
    <button id="go4">取第</button>
    </body>
    </html>
    
  • 相关阅读:
    wifite+aimon-ng
    DC-2
    chrome插件开发
    mongoose的基本操作方法
    webpack中的require.context
    sequelize 数据类型 model
    React17 系统精讲 结合TS打造旅游电商平台
    2021必修 React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目
    21.8.2
    胡渊鸣《浅析信息学竞赛中概率论的基础与应用》学习笔记
  • 原文地址:https://www.cnblogs.com/zlcom/p/3213581.html
Copyright © 2011-2022 走看看