javascript - How to get the index of button in function when I click button? -


<button class="btn btn-warning delcartitem" onclick="delcartitem(3)"> delete </button> <button class="btn btn-warning delcartitem" onclick="delcartitem(3)"> delete </button> ...so on 

i'm making button can delete shopping cart item.

and parameter of delcartitem() product id.

how index of button when click 1 of button in delcartitem() function?

supplement:

this purpose:

when click button => index of button => use $('.item_price').eq(idx).text() price of shopping cart item

<thead> <tr>     <th>         <h3><strong> 項目 </strong></h3></th>     <th>         <h3><strong> 商品編號 </strong></h3></th>     <th>         <h3><strong> 商品名稱 </strong></h3></th>     <th>         <h3><strong> 存貨量 </strong></h3></th>     <th>         <h3><strong> 原價 </strong></h3></th>     <th>         <h3><strong> 數量 </strong></h3></th>     <th>         <h3><strong> 小計 </strong></h3></th>     <th>         <h3><strong> 操作 </strong></h3></th> </tr> </thead> <tbody>     <tr>         <td>2</td>         <td>2</td>         <td>用mblock玩arduino - starting scratch</td>         <td>0</td>         <td class="item_price">300</td>         <td>             <select name="cnt_item[]" class="selectpicker cnt_item" data-width="fit" data-style="btn-default" data-live-search="true"></select>         </td>         <td class="item_total_price">300</td>         <td>             <button class="btn btn-warning delcartitem" id='item1' onclick="delcartitem(this.id, 2)"> <i class="fa fa-times-circle"></i> 刪除 </button>         </td>     </tr>     <tr>         <td>1</td>         <td>1</td>         <td>深入淺出程式設計</td>         <td>9</td>         <td class="item_price">578</td>         <td>             <select name="cnt_item[]" class="selectpicker cnt_item" data-width="fit" data-style="btn-default" data-live-search="true"></select>         </td>         <td class="item_total_price">578</td>         <td>             <button class="btn btn-warning delcartitem" id='item0' onclick="delcartitem(this.id, 1)"> <i class="fa fa-times-circle"></i> 刪除 </button>         </td>     </tr> </tbody> 

[edited]

this should work.

$(".delcartitem").on("click", function(){        var $tr = $(this).closest("tr");        var price = $tr.find(".item_price").text();    var id = $(this).attr('id');        var index = $("tr", $tr.closest("table")).index($tr)         alert("clicked btn in rom: "+ index);        //delcartitem(index);    });            function delcartitem(i){    //alert("deleting item "+i);    return;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>        <table>  <thead>  <tr>      <th>          <h3><strong> 項目 </strong></h3></th>      <th>          <h3><strong> 商品編號 </strong></h3></th>      <th>          <h3><strong> 商品名稱 </strong></h3></th>      <th>          <h3><strong> 存貨量 </strong></h3></th>      <th>          <h3><strong> 原價 </strong></h3></th>      <th>          <h3><strong> 數量 </strong></h3></th>      <th>          <h3><strong> 小計 </strong></h3></th>      <th>          <h3><strong> 操作 </strong></h3></th>  </tr>  </thead>  <tbody>      <tr>          <td>2</td>          <td>2</td>          <td>用mblock玩arduino - starting scratch</td>          <td>0</td>          <td class="item_price">300</td>          <td>              <select name="cnt_item[]" class="selectpicker cnt_item" data-width="fit" data-style="btn-default" data-live-search="true"></select>          </td>          <td class="item_total_price">300</td>          <td>              <button class="btn btn-warning delcartitem" id='item1' onclick="delcartitem(this.id, 2)"> <i class="fa fa-times-circle"></i> 刪除 </button>          </td>      </tr>      <tr>          <td>1</td>          <td>1</td>          <td>深入淺出程式設計</td>          <td>9</td>          <td class="item_price">578</td>          <td>              <select name="cnt_item[]" class="selectpicker cnt_item" data-width="fit" data-style="btn-default" data-live-search="true"></select>          </td>          <td class="item_total_price">578</td>          <td>              <button class="btn btn-warning delcartitem" id='item0' onclick="delcartitem(this.id, 1)"> <i class="fa fa-times-circle"></i> 刪除 </button>          </td>      </tr>  </tbody>  </table>


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -