我已经用PHP和name="student[<?php echo $StudentID ; ?>]"生成了多个文本框。
现在,单击一个按钮,我想使用jquery更改所有这些文本框的值。
我该怎么做呢?请帮帮忙。
发布于 2011-11-05 02:18:28
您可以使用Attribute Starts With selector在student[属性的开头查找名称:
$('input[name^="student["]').val('the new value');可能没有必要在末尾包含[,假设您没有其他名称为student_name或类似名称的输入,name^="student"就足够了。
// If no conflicting named inputs, use
$('input[name^="student"]').val('the new value');发布于 2011-11-05 02:25:22
HTML
<input type="text" name="student[]"></input>
<input type="text" name="student[]"></input>
<input type="text" name="student[]"></input>
<button id="button">Change</button>JavaScript
$('#button').click(function() {
$('input[name^="student"]').val('some value ');
});发布于 2011-11-05 03:07:31
您也可以简单地添加一个对所有这些文本框都是唯一的类(即changableTextBox),然后用它选择它并一次性更改它们。如果你需要一次调整所有的样式,这对将来也很有帮助。只需在CSS中声明该类,即可设置样式。
<input type="text" class="changeableStudentTextBox" id="student[11]" />
<input type="text" class="changeableStudentTextBox" id="student[23]" />
<input type="text" class="changeableStudentTextBox" id="student[45]" />
<input type="text" class="changeableStudentTextBox" id="student[66]" />
<script type="text/javascript">
$('#button').click( function() { $('.changeableStudentTextBox').val('hi!'); });
</script>https://stackoverflow.com/questions/8013960
复制相似问题