首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery :输入选择器,而不是选择:选择元素?

jquery :输入选择器,而不是选择:选择元素?
EN

Stack Overflow用户
提问于 2016-04-07 04:58:03
回答 3查看 578关注 0票数 1

我试图使用jQuery迭代一个输入字段表,获取每个值并连接到一个字符串。在单击submit按钮时,我想连接输入字段,即:input:select。我读到jquery输入选择器也会选择select字段,但这里似乎并非如此。我尝试过将多个类型传递到find()中,但这也不起作用。有小费吗?

这里是jQuery

代码语言:javascript
复制
 $("#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标记

代码语言:javascript
复制
 <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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-07 05:01:30

由于您在这里使用了event.preventDefault();,您应该将$("#SubmitButton").click(function(){更改为$("#SubmitButton").click(function(event){

当您使用eventHandler时,您应该添加一个参数。

在HTML中,您还应该做一个更改,其中使用了选择框,这需要标记,开始和结束,您还没有正确地启动和结束标记,下面是一行

代码语言:javascript
复制
<select class="teamInput" name = "player[]" type="text" placeholder="Male/Female" required</select> <option>Male</option><option>Female</option>  

更改为

代码语言:javascript
复制
<select class="teamInput" name = "player[]" placeholder="Male/Female" required><option>Male</option><option>Female</option></select>

另外,您还可以使用类选择器,在每个元素中,.teamInput都是常见的。

因此,您可以轻松地将其加起来,然后按照jQuery来查看它的工作情况。

代码语言:javascript
复制
$("#SubmitButton").click(function(event){
    event.preventDefault();
    $(".teamInput:input:visible").each(function() {
       alert($(this).val());
    });
});

工频:https://jsfiddle.net/qffL6dsp/1/

票数 3
EN

Stack Overflow用户

发布于 2016-04-07 05:05:18

替换

代码语言:javascript
复制
   $('.NewTeam').eq(i).find('input').each(function () {

用简单的

代码语言:javascript
复制
  $('.NewTeam').eq(i).find(':input').each(function () {

input只选择输入标记,:input也选择其他表单输入元素

票数 0
EN

Stack Overflow用户

发布于 2016-04-07 05:12:06

首先,需要将事件作为参数传递给单击函数,然后修改.each的选择器,以找到select元素,因为' input‘只会找到输入元素,而不是select元素:

代码语言:javascript
复制
$("#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
      });
   }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36467042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档