简体   繁体   English

jquery:输入选择器不选择:选择元素?

[英]jquery :input selector not selecting :select elements?

I am trying to use jQuery to iterate through a table of input fields, taking each value and concatenating to a string. 我试图使用jQuery迭代输入字段表,获取每个值并连接到一个字符串。 On click of the submit button, I would like to concatenate input fields that are :input and :select . 单击提交按钮,我想连接输入字段:input:select I read up that jquery input selector also selects select fields as well, but that doesnt seem to be the case here. 我读到jquery输入选择器也选择了选择字段,但这似乎不是这里的情况。 I have tried passing multiple types into find() but that didnt work either. 我已经尝试将多个类型传递给find()但这也没有用。 Any tips? 有小费吗?

Here is the jQuery 这是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 Markup 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>

As you have used event.preventDefault(); 正如您使用了event.preventDefault(); here, you should change $("#SubmitButton").click(function(){ to $("#SubmitButton").click(function(event){ 在这里,你应该更改$("#SubmitButton").click(function(){ to $("#SubmitButton").click(function(event){

while you use eventHandler, you should add up a parameter. 当您使用eventHandler时,您应该添加一个参数。

In Your HTML, you should also make a change, where you have utilized select box, which requires both the tags, start and end, you haven't started and ended the tag properly, here is that line 在你的HTML中,你也应该进行更改,你已经使用了选择框,这需要标签,开始和结束,你没有开始和正确结束标签,这是该行

<select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option>  

change to 改成

<select class="teamInput" name = "player[]" placeholder="Male/Female" required><option>Male</option><option>Female</option></select>

And Additionally, you can utilize your Class selector, where you have .teamInput as common in every element. 此外,您可以使用类选择器,其中.teamInput在每个元素中都是通用的。

So easily you can just add up, following jQuery to see it working. 你可以轻松地添加,按照jQuery来看它的工作原理。

$("#SubmitButton").click(function(event){
    event.preventDefault();
    $(".teamInput:input:visible").each(function() {
       alert($(this).val());
    });
});

Working Fiddle : https://jsfiddle.net/qffL6dsp/1/ 工作小提琴: https//jsfiddle.net/qffL6dsp/1/

Replace 更换

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

with simply 简单地说

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

input selects only input tags, :input selects other form-input elements as well. input仅选择输入标记, :input选择其他表单输入元素

First you need to pass event as parameter to your click function and then you need to modify the selector for .each to also find select element as 'input' will only find input elements and not select elements : 首先,您需要将event作为参数传递给click函数,然后您需要修改.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
      });
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM