簡體   English   中英

無法使用查找功能獲取jquery中隱藏輸入的值

[英]unable to get value of hidden input in jquery using find function

我想要當用戶單擊“刪除”圖標時,我需要“刪除”類中的隱藏輸入值,但是我無法定義,有人可以指導我我做錯了什么

<table class="items-list">
            <tr>
                <th>&nbsp;</th>
              <th>Product name</th>
              <th>Product price</th>
              <th>Quantity</th>
              <th>Total</th>
            </tr>
            <!--Item-->
            <tr class="item">
                <td class="thumb"><a href="shop-single-item-v1.html"><img src="img/catalog/shopping-cart-thumb_2.jpg" alt="Lorem ipsum"/></a></td>
              <td class="name"><a href="shop-single-item-v1.html">Wristlet</a></td>
              <td class="price">715,00 $</td>
              <td class="qnt-count">
                <a class="incr-btn" href="#">-</a>
                <input class="quantity form-control" type="text" value="2">
                <a class="incr-btn" href="#">+</a>
              </td>
              <td class="total">2715,00 $</td>
              <td class="delete"> <input type="hidden" name="row_id" value="123" > <i class="icon-delete"></i></td>
          </tr>

          </table>

我的jQuery代碼如下

$(document).on('click', '.shopping-cart .delete', function(){
       var $target = $(this).parent().find('.row_id').val();
       alert($target);

});

每次我發出警報時,我都會收到錯誤消息“ undefined。嘗試了許多不同的方法,但是沒有起作用

您在輸入中沒有類,只有一個名稱,像這樣更改標記:

<input type="hidden" name="row_id" class="row_id" value="123">

或這樣的選擇器:

var $target = $(this).parent().find('input[name="row_id"]').val();

 $(document).on('click', '.delete', function(){ var $target = $(this).find('input').val(); alert($target); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="items-list"> <tr> <th>&nbsp;</th> <th>Product name</th> <th>Product price</th> <th>Quantity</th> <th>Total</th> <th>action</th> </tr> <!--Item--> <tr class="item"> <td class="thumb"><a href="shop-single-item-v1.html"><img src="img/catalog/shopping-cart-thumb_2.jpg" alt="Lorem ipsum"/></a></td> <td class="name"><a href="shop-single-item-v1.html">Wristlet</a></td> <td class="price">715,00 $</td> <td class="qnt-count"> <a class="incr-btn" href="#">-</a> <input class="quantity form-control" type="text" value="2"> <a class="incr-btn" href="#">+</a> </td> <td class="total">2715,00 $</td> <td class="delete"> <input type="hidden" name="row_id" value="123" > <i class="icon-delete">Delete</i></td> </tr> </table> 

請檢查工作代碼。

我首先添加了一個新的<TH>標簽來顯示刪除按鈕。 然后在jquery中,我使用.find獲取<TD>的內部元素,並且根據您的代碼,它更改了隱藏框的值

謝謝,希望它可以幫助您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM