Javascript jquery:输入选择器未选择:选择元素?

Javascript jquery:输入选择器未选择:选择元素?,javascript,jquery,html,Javascript,Jquery,Html,我尝试使用jQuery迭代输入字段表,获取每个值并连接到字符串。单击submit按钮后,我想将:input和:select的输入字段连接起来。我读到jquery输入选择器也选择select字段,但这里的情况似乎不是这样。我尝试将多个类型传递到find()中,但这也不起作用。有什么建议吗 这是jQuery的 $("#SubmitButton").click(function(){ var Teams = $(".NewTeam").length; //working event.

我尝试使用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
      });
   }
});
$(“#提交按钮”)。单击(函数(){
var Teams=$(“.NewTeam”).length;//正在工作
event.preventDefault();
警报(“点击让我们做ajax”);
对于(变量i=0;i
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>

玩家信息:
Jersey SizeOuth Small Youth Medium Youth Large Youth X-Largesmall Medium LargeExtra Large
短码Outh Small Youth中码Youth Large青年X-Largesmall中码LargeExtra大码

正如您使用的
event.preventDefault()
在这里,您应该更改
$(“#提交按钮”)。单击(函数(){
$(“#提交按钮”)。单击(函数(事件){

在使用eventHandler时,应该添加一个参数

在你的HTML中,你也应该做一个改变,你使用了选择框,它需要标记,开始和结束,你没有正确地开始和结束标记,这是那一行

<select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option>  
工作小提琴:更换

   $('.NewTeam').eq(i).find('input').each(function () {
简单地

  $('.NewTeam').eq(i).find(':input').each(function () {

input
仅选择输入标记,
:input
也选择其他标记。

首先需要将事件作为参数传递给单击函数,然后需要修改的选择器。要将选择元素作为“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
      });
   }
});
$(“#提交按钮”)。单击(函数(事件){
var Teams=$(“.NewTeam”).length;//正在工作
event.preventDefault();
警报(“点击让我们做ajax”);
对于(变量i=0;i
您是否尝试过
$('.NewTeam:input')。每个(function(){})
?谢谢。工作起来很有魅力。通过传递事件,函数重载到底做了什么?您总是受欢迎@AlimCharaniya。为了解释这一点,我可以简单地提到,它跟踪目标元素的所有类型的活动,正如您所说的
preventDefault()
它避免了此处按钮的默认功能。
  $('.NewTeam').eq(i).find(':input').each(function () {
$("#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
      });
   }
});