我试图使用jQuery迭代一个输入字段表,获取每个值并连接到一个字符串。在单击submit按钮时,我想连接输入字段,即:input和:select。我读到jquery输入选择器也会选择select字段,但这里似乎并非如此。我尝试过将多个类型传递到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>发布于 2016-04-07 05:01:30
由于您在这里使用了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());
});
});发布于 2016-04-07 05:05:18
替换
$('.NewTeam').eq(i).find('input').each(function () {用简单的
$('.NewTeam').eq(i).find(':input').each(function () {input只选择输入标记,:input也选择其他表单输入元素。
发布于 2016-04-07 05:12:06
首先,需要将事件作为参数传递给单击函数,然后修改.each的选择器,以找到select元素,因为' input‘只会找到输入元素,而不是select元素:
$("#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
});
}
});https://stackoverflow.com/questions/36467042
复制相似问题