2017-08-24 41 views
-2

enter image description herejquery如何從最後tr得到值輸入?

   <tr id="subpage_title1"> 
          <td class="order minus">1</td> 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="1" value="3000"></td> 
          <td></td> 
       </tr><tr id="subpage_title2"> 
          <td class="order ">2</td> 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="2" value="3000"></td> 
          <td></td> 
       </tr> 
       <tr id="subpage_title3" style="display: table-row;"> 
          <td class="order">3</td> 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="3" value="3000"></td> 
          <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
       </tr> 
       <tr id="subpage_title4" style="display: table-row;"> 
         <td class="order">4</td><td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
         <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
         <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="4" value="2000"></td> 
         <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
       </tr> 

我累了,從過去的TR獲得價值輸入類=「和」只請了一些一個可以幫助我!

如果我刪除一行,我必須獲得最後的值,如果我添加其他行,我必須獲得最後的值

+1

你不能有一個以上的'hfloor' ID 。 ID意味着獨特。 – Andy

+0

除非頁面上有其他'.sum'元素,只要執行'$('。sum')。last()'應該這樣做? – adeneo

回答

0

console.log($('table tr:last td input.sum').val())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
<tr id="subpage_title1"> 
 
          <td class="order minus">1</td> 
 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
 
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="1" value="3000"></td> 
 
          <td></td> 
 
       </tr><tr id="subpage_title2"> 
 
          <td class="order ">2</td> 
 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
 
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="2" value="3000"></td> 
 
          <td></td> 
 
       </tr> 
 
       <tr id="subpage_title3" style="display: table-row;"> 
 
          <td class="order">3</td> 
 
          <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
 
          <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
 
          <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="3" value="3000"></td> 
 
          <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
 
       </tr> 
 
       <tr id="subpage_title4" style="display: table-row;"> 
 
         <td class="order">4</td><td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
 
         <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
 
         <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="4" value="2000"></td> 
 
         <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
 
       </tr> 
 
       
 
       <table>

嘗試。

+1

糟糕的選擇器。只是這個'$('表tr:最後td input.sum')。val()'會好 – DontVoteMeDown

3

試試這個:

$("tr:last td input.sum") 
+0

不會工作,輸入有'sum'類而不是td – ThunD3eR

+0

你當然是對的,謝謝。固定。 – lancew

1

的HTML:

<table> 
     <tr id="subpage_title1"> 
        <td class="order minus">1</td> 
        <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
        <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
        <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="1" value="3000"></td> 
        <td></td> 
     </tr><tr id="subpage_title2"> 
        <td class="order ">2</td> 
        <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
        <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td>    
        <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="2" value="3000"></td> 
        <td></td> 
     </tr> 
     <tr id="subpage_title3" style="display: table-row;"> 
        <td class="order">3</td> 
        <td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
        <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
        <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="3" value="3000"></td> 
        <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
     </tr> 
     <tr id="subpage_title4" style="display: table-row;"> 
       <td class="order">4</td><td><input type="checkbox" name="checkF[]" checked="" value=""> Front (A)</td> 
       <td><input type="checkbox" name="checkB[]" value=""> Back (C)</td> 
       <td><input id="hfloor" class="sum" name="hfloor[]" type="number" min="100" max="5000" step="4" value="2000"></td> 
       <td><a class="btn-remove-tr btn btn-small">Delete Floor</a></td> 
     </tr> 
    </table>  

腳本:

$(function(){ 
var value = $("tr:last td input.sum").val() 
console.log(value) 
}) 

小提琴here