$(document).ready(function(){
$("#pList").change(function(){
updateCity($("#pList option:selected")[0].value);
});
$("#cList").change(function(){
updateDistrict($("#cList option:selected")[0].value);
});
});pList和cList是2个select标签,现在要给他们加上change事件,代码如上,但是只有pList的事件加上去了。cList的change没效果,求助,依样画葫芦改的,如果这个方法不对还忘各位指教下
1把clist的标签代码发出来
2把updateDistrict代码发出来
<option value="-1">==请选择城市==</option></select>
function updateDistrict(cityId)
{
try
{
//var cityId = $("#cList option:selected")[0].value;
createXMLHttpRequest();
xmlHttp.onreadystatechange = dispDistrict;
xmlHttp.open("POST","httpRequest.aspx?cityId="+cityId);
xmlHttp.send(null);
}
catch(e)
{}
}在调试的时候根本没有执行到updateDistrict
把select的ruant='server'去掉
<option value="-1">==请选择城市==</option></select>或者
<select size="1" id="cList" style="width:120px;" class="cList" runat="server">
<option value="-1">==请选择城市==</option></select> $(".cList").change(function(){
updateDistrict($("#cList option:selected")[0].value);
});
原则上你的jquery写法和内联写法都没错,但是你又说不行,我想知道,你的cList有多少个在这个页面里
$("#pList").change(function(){
updateCity($("#pList option:selected")[0].value);
});
$("#cList").change(function(){
updateDistrict($("#cList option:selected")[0].value);
});
});这里的断点就是执行不到
{
if(xmlHttp.readyState==4)//response successfully
{
if(xmlHttp.status==200)//return successfully
{
// $("#cList").find('option').remove().end().append('<option value="-1">==请选择城市==</option>');
// $('#dList').find('option').remove().end().append('<option value="-1">==请选择区域==</option>');
// $("#cList")[0].outerHTML = $("#cList")[0].outerHTML.replace("</SELECT>", xmlHttp.responseText + "</select>");
// $("#cList").change(function(){updateDistrict($("#cList option:selected")[0].value);});
var option = document.createElement("option");
option.value = "0";
option.text = "测试";
$("#cList")[0].options.add(option);
}
}
}我为了方便点直接传回来的数据格式是<option value=''>text</option>这样的字符串,直接替换outerHTML的</select>部分,这样可能把之前绑定的事件给清理掉了。但是为什么会清理掉呢?难道一定要循环把各项加进去么?