jquery獲取input輸入框中的值、radio單選框選中的值、checkbox複選框選中的值
阿新 • • 發佈:2019-01-06
1)jquery獲取input輸入框中的值
如何用jquery獲取<input id="test" name="test" type="text"/>中輸入的值?
$(" #test ").val()
$(" input[ name='test' ] ").val()
$(" input[ type='text' ] ").val()
$(" input[ type='text' ]").attr("value")
2)jquery獲取radio單選框選中的值
例項1:
<div id="wrap"> <input type="radio" name="payMethod" value="1" />男 <input type="radio" name="payMethod" value="2" />女 </div> 獲取一組單選按鈕物件:var obj_payPlatform = $('#wrap input[name="payMethod"]'); 獲取被選中按鈕的值 :var val_payPlatform = $('#wrap input[name="payMethod"]:checked ').val();
例項2:
使用jquery獲取radio的值,最重要的是掌握jquery選擇器的使用,在一個表單中我們通常是要獲取被選中的那個radio項的值,所以要加checked來篩選,比如有以下的一些radio項:
1.<input type="radio" name="testradio" value="jquery獲取radio的值" />jquery獲取radio的值 2.<input type="radio" name="testradio" value="jquery獲取checkbox的值" />jquery獲取checkbox的值 3.<input type="radio" name="testradio" value="jquery獲取select的值" />jquery獲取select的值
要想獲取某個radio的值有以下的幾種方法,直接給出程式碼:
1.$('input[name="testradio"]:checked').val();
2,$('input:radio:checked').val();
3、$('input[@name="testradio"][checked]');
4、$('input[name="testradio"]').filter(':checked');
差不多挺全的了,如果我們要遍歷name為testradio的所有radio呢,程式碼如下
$('input[name="testradio"]').each(function(){2.alert(this.value);3.});
如果要取具體某個radio的值,比如第二個radio的值,這樣寫
$('input[name="testradio"]:eq(1)').val()
3)jquery獲取checkbox複選框選中的值
<!--html-->
<input type="checkbox" name="test" title="測試1" value="1" lay-skin="primary">
<input type="checkbox" name="test" title="測試2" value="2" lay-skin="primary">
<input type="checkbox" name="test" title="測試3" value="3" lay-skin="primary">
<input type="checkbox" name="test" title="測試4" value="4" lay-skin="primary">
<input type="checkbox" name="test" title="測試5" value="5" lay-skin="primary">
// js
var test_list = []
$("[name=test]:checked").each(function () {
test_list.push($(this).val())
});
test_str = JSON.stringify(test_list ); // 轉換成字json字串