2012-08-16 10 views
0

我試圖在dom中設置下一個輸入,但只是出錯。更改下一個('輸入')失敗的屬性

繼承人的jQuery的:

$('select.expense-code').change(function(e) { 

    // If expense code is travel mileage 
    if ($(this).val() == '5') { 
     // Set miles to read only 
     var currentRowAmount = $(e.target).next("input.amount"); 
     currentRowAmount.attr('readonly', true); 
    } 

}); 

繼承人的DOM的一部分:

DOM

我已經試過只設置背景顏色等,但沒有什麼工作,我敢肯定它的下降到.next()位,但需要正確的方向。 Console.log()只是給你[]。

在此先感謝

回答

2

.next()抓住眼前的下一個兄弟,它不會搜索任何內容,過濾器參數只是爲了確保接下來的兄弟是在過濾器中。

docs

說明:獲取緊隨其後的各元素的同級 該組匹配元件。如果提供了一個選擇器,只有在它與該選擇器匹配時纔會檢索下一個兄弟對象 。

既然你已經有了目標ID,你可以做:

// current id ($(this).prop('id')) is Expense0ExpenseCode 
var curId = $(this).prop('id'); 

// you are targetting Expense0Amount, which has the same Expense0 as the prefix 
// so replace the suffix (ExpenseCode -> Amount): 
var targetId = curId.replace('ExpenseCode', 'Amount'); 

// do the prop change 
$('#' + targetId).prop('readonly', true); 

的一個班輪:

$('#' + $(this).prop('id').replace('ExpenseCode', 'Amount')).prop('readonly', true); 
+0

謝謝你的作品!你能解釋一下你在那裏做什麼 - 努力遵循並想要理解。謝謝 – 2012-08-16 10:54:28

+0

@JamesJ編輯我的答案和解釋。 – 2012-08-16 10:56:37

+0

太棒了,謝謝 – 2012-08-16 11:11:03

0

你的元素不是同級可言,他們是裏面兩個不同的親本.span1元素:

$('select.expense-code').on('change', function(e) { 
    if ($(this).val() == '5') { 
     $(this).closest('.span1') 
       .next('.span1') 
       .find('input.amount') 
       .prop('readonly', true); 
    } 
}); 
相關問題