前几天趴新浪的省份级联信息做的注入代码触发事件
var pro = document.getElementById("province");
var obj = {};
function getValue(){
var index = pro.selectedIndex;
obj[pro.options[index].text] = {
key: index
};
var city =document.getElementById("city");
obj[pro.options[index].text].values = {};
for (var j = 0, len = city.options.length; j < len; j++) {
obj[pro.options[index].text].values[city.options[j].text] = city.options[j].value;
}
}
pro.addEventListener("change", function(){
getValue();
});
for(var i=0;i<pro.options.length;i++){
pro.options[i].selected= true;
ev = document.createEvent("HTMLEvents");
ev.initEvent("change", false, true);
pro.dispatchEvent(ev);
}
console.log(JSON.stringify(obj));