【问题标题】:find().val() returns undefinedfind().val() 返回未定义
【发布时间】:2017-10-08 14:30:48
【问题描述】:

我正在尝试使用 find().val() 在 jquery 数据表中获取动态隐藏输入的值,但它返回未定义。如何使它返回输入的值?

//foreach instantiate
   <input style="display: none;" value="{{$userTable['username']}} - {{$userTable['name']}}" />

//end foreach instantiate

jquery 试图获取隐藏输入的值。

 $.each($("#datacal_table tr"), function(){
        arrTableName.push($(this).find('input').eq(0).val());
      });

表格布局

    <table id="datacal_table" 
     class="table table-striped table-bordered" cellspacing="0">
       <thead>
          <tr>
           <th>Username</th>
           <th>Type</th>
           <th>Name</th>
           <th>Computation Sheet Name</th>
           <th>Status</th>
           <th>Date</th>
         </tr>
      </thead>
      <tbody id="dctbody">
      //foreach instantiation
       <tr>
      <td id="username_table">
          {{$userTable['username']}} <br>
          {{$userTable['name']}}

       <input style="display: none;" value="{{$userTable['username']}}
     - {{$userTable['name']}}" />
     </td>
    ......<td> values
     <tr>
    //end for each
  </tbody>
</table>

【问题讨论】:

  • 段落find('p') 没有值。

标签: javascript jquery laravel


【解决方案1】:

你可以这样使用

 $("#datacal_table > tbody  >").each(function(){
  arrTableName.push($(this).find('input:hidden').val());
  })

使用直接子选择器 (&gt;),您将遍历直接后代(而不是所有后代)

【讨论】:

    【解决方案2】:

    更准确地说,您应该查找tbody 部分tr 元素,并且也应查找input 元素。也没有您应该寻找的 p 元素。

     $.each($("#datacal_table tbody tr"), function(){
        arrTableName.push($(this).find('input').val());
     });
    

    【讨论】:

      【解决方案3】:

      您需要找到input 元素而不是p 标记。虽然 eq() 方法在这里不是必需的,因为 val() 方法返回第一个元素的值。

      arrTableName.push($(this).find('input').val());
      //                           ---^^^^^---
      
      // or more specific to get hidden element
      arrTableName.push($(this).find('input:hidden').val());
      //                           ---^^^^^^^^^^^^^---
      

      仅供参考:input 元素是自动关闭的,因此不需要&lt;/input&gt;

      【讨论】:

        猜你喜欢
        • 2021-09-06
        • 2016-12-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多