$(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.   

    代码么问题 。
    1把clist的标签代码发出来
    2把updateDistrict代码发出来
      

  2.   

    cList<select size="1" id="cList" style="width:120px;" runat="server">
    <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
      

  3.   

    好 我告诉你原因
    把select的ruant='server'去掉
      

  4.   

    <select size="1" id="cList" style="width:120px;" runat="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);
        });
      

  5.   

    请问这是为什么呢?我的pList里面也有runat="server"写法是和cList一样的
      

  6.   

    按你的方法改了。但是还是没有执行,我直接在cList里面加上 onchange="updateDistrict(id)"这样是可以的。
      

  7.   

    刚看了下页面上id没变,不过你说的对,是不能这样,你一提醒才想起来,服务器控件的Id有可能会变。
      

  8.   


    原则上你的jquery写法和内联写法都没错,但是你又说不行,我想知道,你的cList有多少个在这个页面里
      

  9.   

    但是runat="server"去掉了还是不会执行啊,我调试的时候$(document).ready(function(){
        $("#pList").change(function(){
            updateCity($("#pList option:selected")[0].value); 
        });
        $("#cList").change(function(){
            updateDistrict($("#cList option:selected")[0].value);
        });
    });这里的断点就是执行不到
      

  10.   

    知道问题在哪了。是我自己pList的change事件执行后的处理函数没写好function dispCity()
    {
        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>部分,这样可能把之前绑定的事件给清理掉了。但是为什么会清理掉呢?难道一定要循环把各项加进去么?