Jquery获取输入框的值

转:https://blog.csdn.net/weixin_40475396/article/details/79000818

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字符串
 
--------------------- 
作者:微光刺眼丶 
来源:CSDN 
原文:https://blog.csdn.net/weixin_40475396/article/details/79000818 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_36330733/article/details/88322071