2013-10-04 45 views
0

我有動態複選框以及相應的文本輸入。我試圖實現的是當選中複選框時,特定的複選框文本輸入的值會被警告。我似乎無法獲得文本輸入的價值。此刻,所有複選框僅提醒第一個文本框的值而不是正確的值。此外,通過系統默認的文本輸入應該被隱藏,直到複選框被點擊,然後給出了各自的文本字段jquery選擇下一個文本輸入值

http://jsfiddle.net/hpJMN/55/

以上是什麼我試着去實現一個簡單的例子。

<h3>Checkbox Toggle input element</h3> 


<div class="equipment"> 

</div> 


printarr(); 

腳本

$(".equipment input:checkbox").change(function() { 
    alert($(this).siblings("input[type=text]").val()); 
}); 

function printarr() { 
    for (var x = 0; x < 4; x++) { 
     $(".equipment").append(
      '<input type="checkbox" name="drill_eq[]" value="' + x + '">&nbsp;' + (x + 1) + '&nbsp<br>' + '<input type="text" name="qty" id="' + (x + 1) + '" title="Qty needed" size="1" maxlength="2"><br>'); 
    } 
} 

回答

0

更改您的線路從這個:

alert($(this).siblings("input[type=text]").val()); 

這樣:

alert($(this).nextAll("input[type=text]").first().val()); 

的主要區別是使用nextAll,而不是兄弟。 「兄弟姐妹」方法將找到所有兄弟姐妹(之前和之後),而nextAll將找到選定元素之後的所有兄弟姐妹(這是您想要的)。

3

你需要改變你的選擇,以可能somethign像這樣根據您的標記,兄弟姐妹將選擇所有輸入字段(即是兄弟)的和你正在做的eq(0)這將獲得第一個只值(事件與出eq(0)你仍然會第一個只值):

$(this).next().next().val(); 

$(this).nextUntil('input[type=text]').next().val(); //If you are not sure how many other elements may come in between these 

$(this).nextAll('input[type=text]').first().val(); //If you are not sure how many other elements may come in between these 

$(this).nextAll('input[type=text]:first').val(); 

Demo

0

試試這個

alert($(this).nextAll("input:text").get(0).value); 

DEMO

相關問題