2011-11-04 75 views

回答

1

可以使用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'); 
+0

您可能最好在選擇器周圍加上''' – ZenMaster

+0

@ZenMaster yikes,當然。謝謝。 –

+0

@Michael:但是先生,如果我還有一些其他元素也是以名字'student ...'開頭的,那麼我該怎麼辦?我必須使用foreach循環來循環所有學生ID,然後執行此操作:------------:$('input [name =「student [<?php echo $ StudentID;? >]「)。val('new value'); – sqlchild

1

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 '); 
}); 

JSFiddle

+0

]這些名稱在'[]'中包含一個PHP提供的id。 'name =「student [<?php echo $ StudentID;?>]' –

+0

@Michael是的,我發現問題在發佈答案後發生了變化,現在已更新。 – ZenMaster

1

你也可以簡單地添加一個類,它是唯一所有的這些文本框(即。 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> 
相關問題