2017-07-07 127 views
1

代碼獲取多個元素值

HTML

<table> 

    <tr> 
     <td><input type="hidden" name="zxc_0" value="105" quantity="1"></td> 
    </tr> 

    <tr> 
     <td><input type="hidden" name="zxc_1" value="155" quantity="3"></td> 
    </tr> 

    <tr> 
     <td><input type="hidden" name="zxc_2" value="15" quantity="7"></td> 
    </tr> 

    </table> 

<a id="button-generate" class="button" style="">Generate</a> 

問:

上面的代碼是HTML存儲與數據價值,所以我怎麼能使用Java一旦用戶點擊生成按鈕,腳本將值數據存儲到Array中?

回答

2

$("#button-generate").on("click", function(){ 
 

 
    var values = $('input').map(function() { 
 
    return $(this).attr('value'); 
 
    }).toArray(); 
 
    
 
    console.log(values); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_0" value="105" quantity="1"></td> 
 
    </tr> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_1" value="155" quantity="3"></td> 
 
    </tr> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_2" value="15" quantity="7"></td> 
 
    </tr> 
 

 
    </table> 
 

 
<a id="button-generate" class="button" style="">Generate</a>

+1

謝謝:),它的工作。 –

4

如果重新使用jQuery,也許你可以試試這個:

$("#button-generate").on("click", function(){ 
 

 
    var vall = []; 
 
    $.each($("input[name^='zxc_']"), function(){    
 
    vall.push($(this).val()); 
 
    }); 
 
    console.log(vall); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_0" value="105" quantity="1"></td> 
 
    </tr> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_1" value="155" quantity="3"></td> 
 
    </tr> 
 

 
    <tr> 
 
     <td><input type="hidden" name="zxc_2" value="15" quantity="7"></td> 
 
    </tr> 
 

 
    </table> 
 

 
<a id="button-generate" class="button" style="">Generate</a>

http://api.jquery.com/category/selectors/attribute-selectors/