簡體   English   中英

通過jquery獲取除最后一個輸入值

[英]Get input value except the last by jquery

我想通過jquery獲取輸入中除最后一個類.tr中的最后一個輸入之外的所有值,但是它對我不起作用,如何解決呢?

我嘗試為:

演示: http : //jsfiddle.net/d4xZK/

HTML:

<div class="tr">
    <input type="text" value="111">
</div>
<div class="tr">
    <input type="text" value="222">
</div>
<div class="tr">
    <input type="text" value="333">
</div>
<div class="tr">
    <input type="text" value="444">
</div>

jQuery的:

$('.tr').each(function(){
    var mpiVal = $('.tr input').not(':last').val();
    alert(mpiVal)
)}

您可以slice()從最后一個:

$('.tr').slice(0, -1).each(function() {
    var mpiVal = $('input', this).val();
    console.log(mpiVal);
});

演示

$('.tr').not(':last').each(function(){
    var mpiVal = $(this).children('input').val();
    alert(mpiVal)
});

此處演示http://jsfiddle.net/d4xZK/4/

或者,您可以簡單地使用正確的選擇器;

$('.tr:not(:last) input').each(function(){
    var mpiVal = $(this).val();
    alert(mpiVal);
});

最后一行有語法錯誤

  var arr = []
  $('.tr:not(:last)').each(function(){
     arr.push.call(arr,$(this).find('input').first().val());         
  });
  alert(arr);
$(".tr:not(:last) input").map(function(){ return $(this).val() }).get()

返回["111", "222", "333"]

暫無
暫無

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

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