JQuery不适用于1号数组(JQuery doesnt work for array of size 1)

编程入门 行业动态 更新时间:2024-10-17 07:27:05
JQuery不适用于1号数组(JQuery doesnt work for array of size 1)

我有一个输入类型复选框如下:

<tr class=odd> <td align="center"> <input type="checkbox" id="psn" name="psn" value='20500561' /> </td> <td width="75"> 20500561&nbsp; </td> <td> ABCDEFG CO <span style="color:maroon;font-size:8pt;"></span> </td> <td> 715 LOCAL STREET </td> <td> ST.LOUIS </td> <td> MO </td> </tr> <tr class=even > <td align="center"> <input type="checkbox" id="psn" name="psn" value='26736' /> </td> <td width="75"> 26736&nbsp; </td> <td> ABC <span style="color:maroon;font-size:8pt;"></span> </td> <td> 1716 LOCUST ST </td> <td> DES MOINES </td> <td> IA </td> </tr> .....

我有一个提交按钮,点击它时调用这个JQuery,

$('#submitButton').click(function () { var mySelect = checkSelected(); if (mySelect == undefined) { alert("You must select an item to continue."); return false; } var commaSepVals = ''; for(var index=0;index < document.searchCridResults.psn.length; index++ ){ if(document.searchCridResults.psn[index].checked){ alert("checked crid"); commaSepVals += document.searchCridResults.psn[index].value+','; alert("commaSepVals ))) "+commaSepVals); } } commaSepVals = commaSepVals.substr(0,commaSepVals.length -1); var returnValsFromQ = '<c:out value="${sessionScope.returnVals}" />'; var returnArray = returnValsFromQ .split(','); /////////////// } function checkSelected() { var selected = $('input[name=psn]:checked').val(); return selected; }

上面的代码适用于大于1的行但在row = 1时失败。当只有一行时,document.searchCridResults.psn.length未定义。 所以我无法获得已检查项目的值。

I have an input type checkbox as folows:

<tr class=odd> <td align="center"> <input type="checkbox" id="psn" name="psn" value='20500561' /> </td> <td width="75"> 20500561&nbsp; </td> <td> ABCDEFG CO <span style="color:maroon;font-size:8pt;"></span> </td> <td> 715 LOCAL STREET </td> <td> ST.LOUIS </td> <td> MO </td> </tr> <tr class=even > <td align="center"> <input type="checkbox" id="psn" name="psn" value='26736' /> </td> <td width="75"> 26736&nbsp; </td> <td> ABC <span style="color:maroon;font-size:8pt;"></span> </td> <td> 1716 LOCUST ST </td> <td> DES MOINES </td> <td> IA </td> </tr> .....

I have a submit button, which when clicked calls this JQuery,

$('#submitButton').click(function () { var mySelect = checkSelected(); if (mySelect == undefined) { alert("You must select an item to continue."); return false; } var commaSepVals = ''; for(var index=0;index < document.searchCridResults.psn.length; index++ ){ if(document.searchCridResults.psn[index].checked){ alert("checked crid"); commaSepVals += document.searchCridResults.psn[index].value+','; alert("commaSepVals ))) "+commaSepVals); } } commaSepVals = commaSepVals.substr(0,commaSepVals.length -1); var returnValsFromQ = '<c:out value="${sessionScope.returnVals}" />'; var returnArray = returnValsFromQ .split(','); /////////////// } function checkSelected() { var selected = $('input[name=psn]:checked').val(); return selected; }

The above code works fine for rows greater than 1 but fails when row = 1. When there is only one row, document.searchCridResults.psn.length is undefined. So I am not able to get the value of the checked item.

最满意答案

这是一个有趣的情况,答案是:使用更多的jQuery。 :-)

如果你想一直得到一个类似数组的东西(特别是:一个jQuery实例),而不是

document.searchCridResults.psn

使用

$(document.searchCridResults).find("[name=psn]")

问题在于,当只有一个时,document.searchCridResults.psn是对该元素的引用。 当存在多个时,DOM会创建类似于数组的东西( NodeList )。 另一方面,jQuery 总是基于集合(这是它的强大功能)。

例如,创建commaSepVals字符串的循环可以转换为:

commaSepVals = $(document.searchCridResults).find("[name=psn]").filter(function() { return this.checked; }).map(function() { return this.value; }).get().join(",");

实例

$("[name=psn]").on("click", function() {
  var commaSepVals = $("#searchCridResults").find("[name=psn]").filter(function() {
    return this.checked;
  }).map(function() {
    return this.value;
  }).get().join(",");
  $("<p>Selected: " + commaSepVals + "</p>").appendTo(document.body);
}); 
  
<p>Click checkboxes below</p>
<table id="searchCridResults">
  <tbody>
    <tr>
      <td>
        <label>
          <input type="checkbox" name="psn" value='first' />first</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='second' />second</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='third' />third</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='fourth' />fourth</label>
      </td>
    </tr>
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
  
 


附注:在多个元素上使用相同的id值是无效的。 同名是好的,但不是同一个id 。

This is one of those amusing situations where the answer is: Use more jQuery. :-)

If you want to consistently get an array-like thing (specifically: a jQuery instance), instead of

document.searchCridResults.psn

use

$(document.searchCridResults).find("[name=psn]")

The issue is that when there's only one, document.searchCridResults.psn is a reference to that one element. When there's more than one, the DOM creates an array-like thing (a NodeList) instead. jQuery, on the other hand, is always set-based (which is a big part of its power).

For instance, that loop creating the commaSepVals string can be converted to:

commaSepVals = $(document.searchCridResults).find("[name=psn]").filter(function() { return this.checked; }).map(function() { return this.value; }).get().join(",");

Live Example:

$("[name=psn]").on("click", function() {
  var commaSepVals = $("#searchCridResults").find("[name=psn]").filter(function() {
    return this.checked;
  }).map(function() {
    return this.value;
  }).get().join(",");
  $("<p>Selected: " + commaSepVals + "</p>").appendTo(document.body);
}); 
  
<p>Click checkboxes below</p>
<table id="searchCridResults">
  <tbody>
    <tr>
      <td>
        <label>
          <input type="checkbox" name="psn" value='first' />first</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='second' />second</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='third' />third</label>
      </td>
      <td>
        <label>
          <input type="checkbox" name="psn" value='fourth' />fourth</label>
      </td>
    </tr>
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
  
 


Side note: It's invalid to use the same id value on more than one element. The same name is fine, but not the same id.

更多推荐

本文发布于:2023-04-28 04:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:号数   不适用于   JQuery   doesnt   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!