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>
    
  • 相关阅读:
    求24点
    关于参数和返回值的常量性
    点到平面的距离公式
    大端序与小端序
    Quake3中的绝对值函数
    整数超出范围时如何表示?
    关于数组的几道面试题
    在移位数组中查找数
    时间复杂度O(n),空间复杂度O(1)的排序
    C++之对象切割
  • 原文地址:https://www.cnblogs.com/zlcom/p/3213581.html
Copyright © 2011-2022 走看看