0
我有这个jquery.ajax表单,它填充数据库中的数据,并在按钮单击时自动填充表单域。 当表单字段为文本类型时,它工作得很好 但是当表单字段是选择框时它不会自动填充。 这里是我到目前为止的代码..为选择框自动填充表单域
<script type="text/javascript">
$(document).ready(function() {
function myrequest(e) {
var lead_id = $('#lead_id').val();
$.ajax({
method: "GET",
url: "/pdp/fetch-client-data/",
dataType: 'json',
cache: false,
data: {
lead_id: lead_id
},
success: function(responseObject) {
alert('success');
$('#client_name').val(responseObject.client_name);
$('#state').val(responseObject.state);
},
failure: function()
{
alert('fail');
}
});
}
$('#fetchFields').click(function(e) {
e.preventDefault();
myrequest();
});
$("#lead_id").bind("change", function(e)
{
myrequest();
});
});
</script>
<table width="600" align ='center'>
<form action ='<?php echo $SERVER['PHP_SELF']; ?>' method='post'>
<tr>
<td>
<label for="lead_id">Lead id: </label>
<input type="text" name="lead_id" id="lead_id">
<button id="fetchFields">Fetch</button>
</td>
</tr>
<tr>
<td>
Agent: <select name="agent">
<option value="">[Select]</option>
<?php foreach($this->agent_query as $agent){ ?>
<option value="<?php echo $agent['id']; ?>"><?php echo $agent['name'];?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>
<label for="client_name">Client Name: </label>
<input type="text" name="client_name" id="client_name">
</td>
</tr>
<tr>
<td>
<label for="state">State: </label>
<select name="state" id='state'>
<option id='state' value='1'></option>
</select>
</td>
</tr>
<tr>
<td>
# of Policies:
<select name="policies">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="NEXT" name="submit">
</td>
</tr>
</form>
</table>
我的意思是选择选项已重视像TX,NY等on..When点击获取按钮,查询返回TX(对于如),那么所选择的选项应该自动是TX。我会尝试你上面的代码,因为我仍然是新的jQuery ajax – Micheal 2012-02-06 03:27:04
没关系它现在的作品..谢谢.. – Micheal 2012-02-06 15:27:14
最后一个问题虽然..如果查询返回空结果?我如何在表单区域显示错误消息?例如。找不到lead_id。 – Micheal 2012-02-06 15:50:46