我有一個表格選擇,我動態填充:的jQuery:嘗試設置選擇選項
<div id="addRecordForm">
<h2 class='uiblocktitle'>Add Record</h2>
<p class="forminstructions">All entries are required</p>
<form id="userform">
<p class="label">Date:</p><p><?php print $_SESSION['longdateform']; ?></p>
<p class="label"><label for="departments" class="bodylabel">Department:</label></p>
<p>
<select id="departments" name="departments">
<option value="null">Select A Department</option>";
<?php
$query = "SELECT * FROM departments";
$results = $db->getResults($query);
while($row = mysql_fetch_array($results)){
if (count($results) > 0) {
//get department and id
$department = $row['department'];
$deptID = $row['id'];
print "<option value=\"" . $deptID . "\">" . $department . "</option>";
}
}
?>
</select></p>
然後我想設置基於一些用戶數據所選擇的選項。被正確返回
//get user department
$.ajax({
type: "POST",
url: "lib/includes/getUserDepartment.php",
data: "empname=" + empname,
success: function(data){
$("#addRecordForm").slideDown('fast');
$('#departments').val(data).change();
}
});
了var數據和倒在下降的一個值對應,它們都從同一個數據庫表中同一領域的拉動,所以拼寫是不是一個問題。出於某種原因,我無法將選中的選項轉換爲正確的選項。我甚至努力在沒有運氣的情況下努力編碼值。我嘗試過$(「#departments」)的變體,val(),使用選項索引等等,nada。我究竟做錯了什麼?謝謝。
.change();是我想念的東西:) – VladL 2013-08-30 08:29:32