#1.去除select控件重复的option
<select id="companyId" onchange="getContract()" name="company"
class="form-control m-bot15" placeholder="请选择" style=" 200px;">
<option value="">请选择</option>
</select>
/* 填充用户单位名称 */
function getCompany() {
$.ajax({
url : "findCompany.json",
type : "POST",
success : function(data) {
var Str = JSON.stringify(data);
alert(Str + "------返回结果-------");
for (var i = 0; i < data.businessDeployList.length; i++) {
var option = document.createElement("option");
var obj = data.businessDeployList[i].company;
$(option).val(obj);
$(option).text(obj);
$('#companyId').append(option);
/* 去除重复的option值 */
if ($("#companyId option:contains(" + obj + ")").length > 1) {
console.log(obj);
$("#companyId option:contains(" + obj+ "):gt(0)").remove();
}
}
},
});
};
参考链接:http://blog.csdn.net/u012767607/article/details/61927808
---------------------------------------------------------------------------------------------------------------------------------------------
#2.ajax返回数据作为全局变量,自动填充数据
/* 动态填充用户单位名称 */
var result; //定义全局变量
function getCompany() {
$.ajax({
url : "findCompany.json",
type : "POST",
async : false,
success : function(data) {
/* var Str = JSON.stringify(data);
alert(Str + "------查看返回结果-------"); */
result=data;
for (var i = 0; i < data.businessDeployList.length; i++) {
var option = document.createElement("option");
var obj = data.businessDeployList[i].company;
$(option).val(obj);
$(option).text(obj);
$('#companyId').append(option);
if ($("#companyId option:contains(" + obj + ")").length > 1) {
console.log(obj);
$("#companyId option:contains(" + obj+ "):gt(0)").remove();
}
}
},
});
};
/* 根据单位名称自动填充合同名称 */
function getContract(r){
var name=$("#companyId").find("option:selected").val();
$("#contractId").empty();
for(var i=0;i < result.businessDeployList.length; i++){
if(name==result.businessDeployList[i].company){
var contract=result.businessDeployList[i].contract;
var option = document.createElement("option");
$(option).val(contract);
$(option).text(contract);
$('#contractId').append(option);
}
}
}