当用户在输入字段中输入内容时,我正在尝试选择动态 ID。我的应用程序输出以下格式的输入字段:
<input id="student_1_first_name" />
<input id="student_1_last_name" />
<input id="student_2_first_name" />
<input id="student_2_last_name" />
<input id="student_3_first_name" />
<input id="student_3_last_name" />
etc.
例如,我尝试这样做来选择 id 字符串的结尾:
<script type="text/javascript">
$(document).ready(
function (){
$("input[id$=_first_name]").bind("keyup", run_some_function_here);
run_some_function_here();
$("input[id$=_last_name]").bind("keyup", run_some_function_here);
run_some_function_here();
}
);
</script>
当我这样做时,Jquery 似乎无法选择输入 ID,因此函数不会运行。您对我如何正确选择 ID 有什么想法吗?
最佳答案
为每个输入分配类别,例如 <input id="student_1_first_name" class="input-class" />
然后试试这个
$(function() {
$('input.input-class').each(function() {
var id = $(this).attr('id');
});
});
关于javascript - 如何选择这种格式的动态 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2671996/