這是我第一次使用JQuery AJAX,所以我對語法不是很熟悉。現在我從數據庫中提取一組值並填充下拉框。我需要AJAX做的是當他們從下拉框中選擇時,用硬編碼信息填充其他三個字段。一旦我得到AJAX正常工作,然後我想查詢數據庫並根據他們從下拉列表中選擇返回結果。JQuery AJAX - 填充文本框和dropbown框
<div class="panel-body">
<div class="form-group">
<label for="nomName" class="col-sm-3 control-label">Name:</label>
<div class="col-sm-8">
<input type="text" class="form-control" data-validation="required" name="nomName" id="nomName" placeholder="Name" maxlength="50">
</div>
</div>
<div class="form-group">
<label for="nomTitle" class="col-sm-3 control-label">Title:</label>
<div class="col-sm-8">
<input type="text" class="form-control" data-validation="required" name="nomTitle" id="nomTitle" placeholder="Title" maxlength="50">
</div>
</div>
<div class="form-group">
<label for="nomDept" class="col-sm-3 control-label">Department:</label>
<div class="col-sm-8">
<select class="form-control" name="nomDept" id="nomDept">
<option value="" disabled selected>Select a Department...</option>
<option value="Building Services">Building Services</option>
<option value="Construction Management">Construction Management</option>
</select>
</div>
</div>
<div class="form-group">
<label for="nomGUID" class="col-sm-3 control-label">AU Email/GUID:</label>
<div class="col-sm-8">
<select class="form-control" name="nomGUID" id="nomGUID">
<option value="" disabled selected>Select a Person...</option>
<?php
while($row = mssql_fetch_array($user_list)){
echo "<option value=\"" . $row['id'] . "\">" . $row['id'] . "</option>";
}
?>
</select>
</div>
</div>
</div>
這裏是我的AJAX。我知道這是不正確的,所以如果他們提供解決方案,我會很欣賞某人的解決方案。謝謝。
$(function() {
var options = {
"Option 1": "value 1",
"Option 2": "value 2",
"Option 3": "value 3"
}
$('#nomGUID').change(function() {
$.ajax({
url: 'test.php',
success: function('#options') {
$('#nomDept').empty();
$('#nomTitle').html('Test AJAX');
}
})
}
}
謝謝!儘管我不得不稍微編輯一下,但這最終爲我工作。這裏有一個小提琴,以防任何人感興趣https://jsfiddle.net/collint25/bc8qdk3n/1/ – collint25