我有以下的代碼,它工作正常:jQuery的 - 用同一類中選擇元素,功能只有
$(function() {
$('select').on('change', function(event){ // This binds listeners to the change event on all the select elements
var sId = this.id; // store off the changed element id
var vId = this.value; // store off the changed element value
var nId = this.name; // store off the changed element name
alert(sId + nId + vId); // for testing
$('select').each(function(){ // this loops across the same set of elements
if(this.name != nId && this.value == vId) { // If it is not the triggering element and the value is the same, do something
this.options.selectedIndex = 0; // reset the value to 'rank'
}
});
});
});
此代碼對頁面上的所有下拉菜單 - 我試圖做的是限於只有類dropdown_1
的下拉菜單 - 這可能嗎?
所以使用選擇你想要的元素選擇。 https://api.jquery.com/class-selector/ – epascarello