1.城市:new_city
2.省份:new_province
function onload(){//绑定窗体的加载事件
var FromType = Xrm.Page.ui.getFormType();
if(FromType==1){//FromType=1是表示在创建的时候
cityLookup();
}else if (FromType == 2){//FromType=2是表示在查看详情的时候
cityLookup();
}
}
function cityLookup() {//绑定到城市的onchange 事件
Xrm.Page.getControl("new_city").addPreSearch(function () {
addprovince();
})
}
function addprovince() {
debugger;
var gl = Xrm.Page.getAttribute("new_province").getValue(); //省份取值
var fetchXml = "";
var a=null;
if (gl != null || gl != "") {//判断省份的值是否为空
//把省份的值带入Featchxml语句查询
fetchXml = "<filter type='and'><condition attribute='new_province' operator='eq' value='" + gl[0].id + "'/></filter>";
}
else {
//将城市的值变成空
fetchXml = "<filter type='and'><condition attribute='new_province' operator='null' /></filter>";
Xrm.Page.getControl("new_city").setValue(a);
}
Xrm.Page.getControl("new_city").addCustomFilter(fetchXml); //为城市赋值
}
//省份province无数据,则城市也变成空
function addprovinceNUll(){//绑定到省份的onchange 事件
debugger;
var province = Xrm.Page.getAttribute("new_province").getValue(); //省份取值
var provincenull=null;
if(province==null||province==""){//判断省份是否为空
Xrm.Page.getAttribute("new_city").setValue(provincenull); //为城市赋值
}
}