jquery:输入选择器未选择:选择元素
jquery :input selector not selecting :select elements?
我正在尝试使用jQuery迭代一个输入字段表,获取每个值并连接到一个字符串。单击提交按钮后,我想连接:input
和:select
的输入字段。我读到jquery输入选择器也选择选择字段,但这里的情况似乎并非如此。我尝试过将多个类型传递到find()
中,但也不起作用。有什么建议吗?
这是jQuery
$("#SubmitButton").click(function(){
var Teams = $(".NewTeam").length; //working
event.preventDefault();
alert("clicked lets do ajax");
for (var i=0; i < Teams; i++){
$('.NewTeam').eq(i).find('input').each(function () {
alert(this.value); // "this" is the current element in the loop
});
}
});
HTML标记
<div class = "teams">
<fieldset class="vfb-fieldset vfb-fieldset-1 str8-sports-roster-upload NewTeam" style = "display: none">
<table class = "PlayerInfoTable">
<tr class = "PlayerRow">
<td><strong style = "vertical-align:top">Player Info: </strong></td>
<td><input class="teamInput" name = "player[]" type="text" placeholder="Player Full Name" required/></td>
<td><input class="teamInput" name = "player[]" type="text" placeholder="Player Number" required/> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Jersey Size" required/><option selected = "selected">Jersey Size</option><option>Youth Small</option><option>Youth Medium</option><option>Youth Large</option><option>Youth X-Large</option><option>Small</option><option>Medium</option><option>Large</option><option>Extra Large</option> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Short Size"><option selected = "selected">Short Size</option><option>Youth Small</option><option>Youth Medium</option><option>Youth Large</option><option>Youth X-Large</option><option>Small</option><option>Medium</option><option>Large</option><option>Extra Large</option></select> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option> </td>
</tr>
</table>
</fieldset>
<fieldset class="vfb-fieldset vfb-fieldset-1 str8-sports-roster-upload NewTeam" style = "display: none">
<table class = "PlayerInfoTable">
<tr class = "PlayerRow">
<td><strong style = "vertical-align:top">Player Info: </strong></td>
<td><input class="teamInput" name = "player[]" type="text" placeholder="Player Full Name" required/></td>
<td><input class="teamInput" name = "player[]" type="text" placeholder="Player Number" required/> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Jersey Size" required/><option selected = "selected">Jersey Size</option><option>Youth Small</option><option>Youth Medium</option><option>Youth Large</option><option>Youth X-Large</option><option>Small</option><option>Medium</option><option>Large</option><option>Extra Large</option> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Short Size"><option selected = "selected">Short Size</option><option>Youth Small</option><option>Youth Medium</option><option>Youth Large</option><option>Youth X-Large</option><option>Small</option><option>Medium</option><option>Large</option><option>Extra Large</option></select> </td>
<td><select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option> </td>
</tr>
</table>
</fieldset>
</div>
由于您在此处使用了event.preventDefault();
,因此应该将$("#SubmitButton").click(function(){
更改为$("#SubmitButton").click(function(event){
使用eventHandler时,应该添加一个参数。
在你的HTML中,你也应该做出改变,你使用了选择框,它需要标签,开始和结束,你没有正确地开始和结束标签,这是行
<select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option>
更改为
<select class="teamInput" name = "player[]" placeholder="Male/Female" required><option>Male</option><option>Female</option></select>
此外,您还可以使用类选择器,其中.teamInput
在每个元素中都是公共的。
所以你可以很容易地加起来,按照jQuery查看它的工作情况。
$("#SubmitButton").click(function(event){
event.preventDefault();
$(".teamInput:input:visible").each(function() {
alert($(this).val());
});
});
工作Fiddle:https://jsfiddle.net/qffL6dsp/1/
更换
$('.NewTeam').eq(i).find('input').each(function () {
只需
$('.NewTeam').eq(i).find(':input').each(function () {
input
只选择输入标签,:input
也选择其他形式的输入元素。
首先,您需要将事件作为参数传递给您的点击函数,然后您需要修改的选择器。每个也要查找select元素作为"input"的元素只会查找输入元素,而不会选择元素:
$("#SubmitButton").click(function(event){
var Teams = $(".NewTeam").length; //working
event.preventDefault();
alert("clicked lets do ajax");
for (var i=0; i < Teams; i++){
$('.NewTeam').eq(i).find('input, select').each(function () {
alert(this.value); // "this" is the current element in the loop
console.info($(this).text() ); this will print in console in blue color the text of <option> as you have not assigned value attribute
});
}
});
相关文章:
- WebdriverIO waitForExist()选择元素's选定的选项
- 如何选择元素的第n个子元素
- Angular没有根据模型更新我的选择元素
- jquery使用name from变量按类选择元素
- 如何使用jQuery按数据日期属性选择元素
- 向html选择元素添加选项
- 使用Jquery在相同类型的元素中选择元素
- 他们是如何使用angular/jqLite find()方法按属性名称和值选择元素的?ng conf 2015
- 如何在Angular中清除选择元素中的过滤器
- 使用其中一个元素作为参考点,从Javascript数组中选择元素
- Javascript选择元素到字符串的比较
- Angular始终选择选择元素中的第一个选项
- 使用 jquery .find() 遍历按类和存储属性选择元素
- 通过 id json, jquery 选择元素
- 如何基于另一个已经存在的选择器选择元素
- 禁用多个选择元素中的非“选定”选项,但使用 jquery 的一个除外
- HTML 选择元素的只读等效项
- 在iPad上,如何通过Javascript事件在选择元素上设置focus()而不显示选项
- 单击事件似乎不适用于 IE 中的选择元素
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载