我想避免为不同的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以及。
谢谢。
为什么不能使用类名将元素与共享行为关联? –