Home  >  Article  >  Web Front-end  >  jQuery gets the corresponding Input example

jQuery gets the corresponding Input example

巴扎黑
巴扎黑Original
2016-11-25 13:52:251421browse

jQuery to get the corresponding Input example:

There are many input boxes on the page, using the EasyUI style, and there are also various other Input boxes without IDs in the middle, as follows:

<input class="easyui-textbox"/>
<input id="holdercscname" class="easyui-textbox" validType="length[0,100]"/>
<input id="holdercscno" class="easyui-textbox" validType="length[0,100]"/>
<input id="managecom" class="easyui-combobox" data-options="editable:false"/>
<input class="easyui-textbox"/>
<input class="easyui-textbox"/>
<input id="starttime" class="easyui-datebox" data-options="editable:false/>
<input class="easyui-textbox"/>
如何获取只获取id属性赋值了的input框呢?
var inputElemts = $(&#39;:input[id]&#39;);    //获取表单所有的input框,并且拥有属性id的
inputElemts.each(function(i,inputElement){
    exportUrl += &#39;&&#39; + $(this).attr(&#39;id&#39;) + &#39;=&#39; + $(this).textbox(&#39;getValue&#39;);
});
补充:
$(&#39;:input&#39;)                获取所有input框
$(&#39;:input[id]&#39;)            获取所有input框,拥有id属性
$(&#39;:input[id^="aaa"]&#39;)    获取所有input框,id为aaa开头  
$(&#39;:input[id$="aaa"]&#39;)    获取所有input框,id为aaa结尾
$(&#39;:input[id*="aaa"]&#39;)    获取所有input框,id包含aaa
$(&#39;:input[id^="aaa"][id$="aaa"]&#39;)    获取所有input框,id为aaa开头,并且id为aaa结尾  
jquery中:input和input的区别
:input表示选择表单中的input,select,textarea,button元素,input仅仅选择input元素。
示例测试:
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
$(function(){
 var len1=$("#form1 input").length;
 var len2=$("#form1 :input").length;
 alert("len1的长度是:"+len1+",len2的长度是:"+len2);
});
</script>
</head>
<body>
<form id="form1" action="#" method="post">
  <label for="userName">用户名:</label>
  <input type="text" id="userName"/>
  <label for="pwd">密码:</label>
  <input type="password" id="pwd"/>
  <select><option>wo kao</option><option>ni  kao</option><option>quancun kao</option></select>
  <textarea cols="15" rows="20"></textarea>
</form>
</body>

Execution results:

len1结果为2,
len2结果为4.

Above It is the content of jQuery to obtain the corresponding Input example. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn