获取radio、select、checkbox标签选中的值

1 <input type="radio" id="radio1" name="radio"><label for="radio1">Choice 1</label>
2 <input type="radio" id="radio2" name="radio" checked="checked"><label for="radio2">Choice 2</label>
3 <input type="radio" id="radio3" name="radio"><label for="radio3">Choice 3</label>

获取radio标签选中的代码:$('input[name="radio"]:checked') --注:name="radio"上面radio标签的name是"radio"

获取radio标签选中的值的代码:$('input[name="radio"]:checked').next().text();因为上面radio标签中的是写作它后面的label标签中,所以通过next()获取到它后面的label标签,最后用text()获取选中radio的值。如果radio标签的值写input标签中,直接用$('input[name="radio"]:checked').val()获取值即可。

注:如果设置第一个radio为选中,代码:$('#radio1').attr('checked','checked');

1 1 <select id="selectmenu">
2 2     <option>Slower</option>
3 3     <option>Slow</option>
4 4     <option>Medium</option>
5 5     <option>Fast</option>
6 6     <option>Faster</option>
7 7 </select>

获取select标签选中的代码:$('#selectmenu option:selected')

获取select标签选中值的代码:$('#selectmenu option:selected').text();option设置value时,可通过$('#selectmenu option:selected').val()获取。

<input type="checkbox" name="test" value="0" />0 
<input type="checkbox" name="test" value="1" />1 
<input type="checkbox" name="test" value="2" />2 
<input type="checkbox" name="test" value="3" />3 
<input type="checkbox" name="test" value="4" />4 
<input type="checkbox" name="test" value="5" />5 
<input type="checkbox" name="test" value="6" />6 
<input type="checkbox" name="test" value="7" />7 

获取checkebox标签选中的代码:$('input[name="test"]:checked')

checkebox标签是可以多选的,一般都配合each函数来遍历所有选中的值:$('input[name="test"]:checked').each(function(){//.....做相应的操作$(this).val()当前遍历到的选中checkbox的值});

 

posted @ 2017-02-08 15:35  周劭洋  阅读(543)  评论(0编辑  收藏  举报