使用下面的jquery脚本我想更新以下select(值和文本):
<select id="training_field" name="training_field">
<option value="">All training fields</option>
<option value="engineering">Engineering</option>
<option value="management">Management</option>
<option value="project_management">Project Management</option>
</select>
jquery的:
<script type="text/javascript">
$(document).ready(function(){
$.post( 'mySQL_UpdateTrainingField.php', {},
function( data ){ // a function to deal with the returned information
for (var i = 0; i < data.length; i++) {
var fieldvalue = data[i];
// HERE I would like to update the value of #training_field options to data[i] and the text to data[i] as well
}
}, "json");
});
</script>
mySQL_UpdateTrainingField.php是一个返回文本数组的脚本。
我该怎么做? 我试过了:
$('#training_field option').attr("value",fieldvalue);
$('#training_field option').text(fieldvalue);
但是这改变了所有选项到数组的最后一个元素。
答案 0 :(得分:0)
,像这样:
$("<option />").attr("value", yourvalue).text(yourvalue).appendTo("#training_field");
yourvalue等于您要填充的 thing 。