我想避免爲不同的ID具有相同的代碼。就像submitter1,submitter2等。在同一個過程中,讓變量也用來獲取不同地點1,不同地點2的輸入框ID。有人可以幫我解決下面的代碼嗎?JQuery和可變ID名稱
$(document).ready(function() {
$('#submitter<--Variable Here-->').click(function(event) {
event.preventDefault();
var newlocation = $("input[name=differentlocation"<--Same Variable Here-->"']");
alert(newlocation); //testing purposes
//$('#location').val(newlocation).change(); //Changes location input box
//$("#submit").click(); //submits new location
});
});
此代碼使用靜態ID,但正如我所說我想調整它使用變量ID以及。
謝謝。
爲什麼不能使用類名將元素與共享行爲關聯? –