[英]jQuery: select ID in list
請原諒我缺乏關於該主題的知識,jQuery不是我的強項!
我正在編輯一個下拉調查表邏輯,當選擇某些答案時,該邏輯僅允許輸入到相鄰的文本字段中。
var allDropdowns = $('.accordion').find('select');
allDropdowns.each(function(index) {
$(this).change(function() {
if ($(this).val() == 2 || $(this).val() == 4) {
allComments.eq(index).removeAttr('disabled');
allComments.eq(index).removeClass('disabled');
allComments.eq(index).attr('placeholder', 'Please provide a comment');
} else {
allComments.eq(index).attr('disabled', 'disabled');
allComments.eq(index).addClass('disabled');
allComments.eq(index).removeAttr('placeholder');
}
});
});
<select id="answer[1]" name="answer[1]">
<option value="0" selected="selected"></option>
<option value="1">Yes</option>
<option value="2">Partial</option>
<option value="3">No</option>
<option value="4">N/A</option>
所以我想做的是添加一個附加條件:如果select id =“ answer [foo]” && $(this).val()== 1,則啟用注釋。
為偽代碼示例道歉!
您可以通過以下方式獲取ID
var id = this.id;
之后,您可以比較您的ID並啟用/禁用評論。
因此,您更新的代碼可能如下所示:
allDropdowns.each(function(index) {
$(this).change(function() {
var $this = $(this),
id = this.id;
if ($this.val() == 2 || $this.val() == 4) {
allComments.eq(index).removeAttr('disabled');
allComments.eq(index).removeClass('disabled');
allComments.eq(index).attr('placeholder', 'Please provide a comment');
} else {
allComments.eq(index).attr('disabled', 'disabled');
allComments.eq(index).addClass('disabled');
allComments.eq(index).removeAttr('placeholder');
}
if(id == 'id_you_are_looking_for' && $this.val() == 1){
// enable comments
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.