如何根据Ajax请求更改选择框所选选项
How to change select box selected option depending on Ajax request
我有一个包含文本框和选择框的表单。我正在向PHP页面发送Ajax请求,并根据该请求自动填写此表单。填写文本框没有问题,但如何确定所选的选择框选项?任何帮助都会得到通知。
$(document).ready(function(){
$("#companyFillSelectBox").change(function(){
var value = $("select#companyFillSelectBox option:selected").val();
$.ajax({
type: 'POST',
url: 'companyFill.php',
dataType: "json",
data:{companyID:value},
success:function(answer){
$('#companyNameText').val(answer[0].companyName);
$('#companyAddressTextArea').val(answer[0].companyAddress);
$('#companyNetAdressText').val(answer[0].companyWebAddress);
$('#companyPhoneText').val(answer[0].companyPhone);
$('#companyFaxText').val(answer[0].companyFax);
$('#companyCeoText').val(answer[0].companyCeo);
$('#companyHRText').val(answer[0].hrDirector);
$('#numOfIengText').val(answer[0].numOfIENG);
$('#numOfEngText').val(answer[0].numOfENG);
$('#numOfWhiteCollarsText').val(answer[0].numOfIENG);
$('#totalEmpText').val(answer[0].numOfEmployees);
$('#establishmentYearText').val(answer[0].yearOfEstablishment);
$("#fieldOfBusinessSelectBox option[value=answer[0].yearOfEstablishment]").attr('selected', 'selected');
},
error:function(){
alert("An error has occured !");
}
});
});
});
要按照您尝试的方式进行操作,只需进行语法修复:
$("#fieldOfBusinessSelectBox option[value=" + answer[0].yearOfEstablishment +"]").attr('selected', 'selected');
然而,你可以像做其他所有事情一样做:
$("#fieldOfBusinessSelectBox").val(answer[0].yearOfEstablishment);
相关文章:
- CKeditor:更改对话框中的默认选择选项
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- AngularJs列表ng单击以选择选项转换
- 使用基于文本的 Jquery 选择选项
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 在php和mysql中选择选项.一旦选择了选项,就列出我的sql数据库数据
- 选择选项时如何显示警报
- 使用php动态更改选择选项
- 选择“选择选项”时显示文本.怎么做
- LEAFLET地图和带有选择选项的标记-ANGULARjs
- 使用js将动态内容添加到选择选项列表中
- 用于使用javascript循环选择选项
- AngularJS-选择选项并在函数中使用它
- 禁用/启用基于单选/选择选项所选Javascript的文本框
- 如何使用jQuery隐藏与选择选项相关的选择选项列表
- 选择选项是添加或删除事件
- 使用jQuery向“选择选项”添加默认属性
- 构建HTML选择字段并使用JavaScript数组选择选项
- 带有动态选择选项的Firefox html5验证异常
- 只有当Angular从资源对象检索数据时,才选择选项元素