首页 > 代码库 > jQuery操作radio、checkbox、select示例

jQuery操作radio、checkbox、select示例

<script src=http://www.mamicode.com/ "../jquery-1.11.1.min.js" ></script>//引入jQuery插件
 
<input type= "radio" name= "radio" id= "radio1" value=http://www.mamicode.com/ "1" />1   
<input type= "radio" name= "radio" id= "radio2" value=http://www.mamicode.com/ "2" />2   
<input type= "radio" name= "radio" id= "radio3" value=http://www.mamicode.com/ "3" />3   
<input type= "radio" name= "radio" id= "radio4" value=http://www.mamicode.com/ "4" />4  
 
<br/>
 
<input type= "checkbox" name= "checkbox" id= "checkbox_id1" value=http://www.mamicode.com/ "1" />1   
<input type= "checkbox" name= "checkbox" id= "checkbox_id2" value=http://www.mamicode.com/ "2" />2   
<input type= "checkbox" name= "checkbox" id= "checkbox_id3" value=http://www.mamicode.com/ "3" />3   
<input type= "checkbox" name= "checkbox" id= "checkbox_id4" value=http://www.mamicode.com/ "4" />4   
<input type= "checkbox" name= "checkbox" id= "checkbox_id5" value=http://www.mamicode.com/ "5" />5   
 
<br/>
 
<select name= "select" id= "select_id" style= "width: 100px;" >   
     <option value=http://www.mamicode.com/ "1" >1</option>   
     <option value=http://www.mamicode.com/ "2" >2</option>   
     <option value=http://www.mamicode.com/ "3" >3</option>   
     <option value=http://www.mamicode.com/ "4" >4</option>   
     <option value=http://www.mamicode.com/ "5" >5</option>   
     <option value=http://www.mamicode.com/ "6" >6</option>   
</select>  
 
<br/>
 
<span onclick= "show()" >点击</span>
 
<script  type="text/javascript">
 
function show()
{
     //jquery获取单选框的值
     alert($( ‘input[type="radio"][name="radio"]:checked‘ ).val());
 
     //jquery循环输出选中的值
     $( "input[type=checkbox][name=checkbox]:checked" ).each( function (){
         alert($( this ).val());   
     });   
 
     //jquery获取Select选中项的Value   
     alert($( "#select_id" ).val());
 
     //jquery获取Select选中项的Text              
     alert($( "#select_id :selected" ).text());   
}
 
</script>