我已經使用PHP生成了多個文本框,其中name="student[<?php echo $StudentID ; ?>]"
。使用jquery更改多個動態PHP生成的文本框的值
現在在一個按鈕上單擊我想要使用jquery更改所有這些文本框的值。
我該怎麼做?請幫忙。
我已經使用PHP生成了多個文本框,其中name="student[<?php echo $StudentID ; ?>]"
。使用jquery更改多個動態PHP生成的文本框的值
現在在一個按鈕上單擊我想要使用jquery更改所有這些文本框的值。
我該怎麼做?請幫忙。
可以使用Attribute Starts With selector,尋找student[
的name屬性的開頭:
$('input[name^="student["]').val('the new value');
它可能不需要包括[
末,和name^="student"
就足夠了,假設你不沒有其他名稱如student_name
等的輸入。
// If no conflicting named inputs, use
$('input[name^="student"]').val('the new value');
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 ');
});
]這些名稱在'[]'中包含一個PHP提供的id。 'name =「student [<?php echo $ StudentID;?>]' –
@Michael是的,我發現問題在發佈答案後發生了變化,現在已更新。 – ZenMaster
你也可以簡單地添加一個類,它是唯一所有的這些文本框(即。 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>
您可能最好在選擇器周圍加上''' – ZenMaster
@ZenMaster yikes,當然。謝謝。 –
@Michael:但是先生,如果我還有一些其他元素也是以名字'student ...'開頭的,那麼我該怎麼辦?我必須使用foreach循環來循環所有學生ID,然後執行此操作:------------:$('input [name =「student [<?php echo $ StudentID;? >]「)。val('new value'); – sqlchild