<select id="promanagername" name="promanagername" class="select"></select>我是通过json从数据库中获取值的,我想怎么在修改页面加载时,将下拉框的值匹配到数据库中,再选中;如果数据库字段值为"黄山",那么下拉框就选中"黄山"

解决方案 »

  1.   

    找到对应的option dom,然后selected
      

  2.   

    $(document).ready(function()
            {
                if(Request("id").length>0)
                {
                    $("#loanBillName").attr("style","color:black;");
                    $.post("loanBill.aspx?method=GetLoanBillByID&lid="+Request("id"),function(result)
                    {                 
                        if(result.length==0){return;}
                        try{eval("("+result+")");}catch(ex){alert(ex.message);}
                        var json=eval("("+result+")");
                           $("#loanBillName").val(json.loanbillname);
                           $("#amountOfMoney").val(json.amountofmoney);
                           $("#applyDate").val(json.applydate.substring(0,10));
    //                       $("select[name='reviewperson'][value=" + json.promanagername + "]").attr("selected",true);
                           $("#bankrollPurpose").val(json.bankrollpurpose);
                           $("#re").val(json.re);
                           $("#lid").val(Request("id"));
                    });
                }
                GetSystemDate();
                BindAllUserName();
            });function BindAllUserName()
            {
                 $("#reviewperson").append("<option>正在加载...</option>");
                 $.post("loanBill.aspx?method=GetAllUserName",function(depart){
                     if(depart.length==0)
                     {return;}
                     try{eval("("+depart+")");}catch(ex){alert(depart);return;}
                     $("#reviewperson").empty();
                     $("#reviewperson").append("<option value=''>全部</option>");
                    
                     var json=eval("("+ depart +")");
                     for(var i=0;i<json.depart.length;i++)
                     {
                         $("#reviewperson").append("<option value='"+json.depart[i].username+"'>"+json.depart[i].username+"</option>");
                     }
                 });
            }
    <select id="reviewperson" name="reviewperson" class="select"></select>
      

  3.   

    两种情况,你看你是哪种吧<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>JavaScript</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script  type="text/javascript">$(function(){
        json = {'promanagername':'黄山'};
        $("select[name='promanagername'] option:contains('" + json.promanagername + "')").attr("selected","selected");
    })
    </script>
    </head>
    <body>
    <select id="promanagername" name="promanagername" class="select"><option value="5">不显示删除回复</option><option value="0">显示所有回复</option><option value="1">显示星级回复</option><option value="3">显示得分回复</option><option value="4">黄山</option></select>
    </body>
    </html> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>JavaScript</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script  type="text/javascript">$(function(){
        json = {'promanagername':'黄山'};
        $("select[name='promanagername'] option[value='" + json.promanagername+ "']").attr("selected","selected");
    })
    </script>
    </head>
    <body>
    <select id="promanagername" name="promanagername" class="select"><option value="5">不显示删除回复</option><option value="0">显示所有回复</option><option value="1">显示星级回复</option><option value="3">显示得分回复</option><option value="黄山">黄山</option></select>
    </body>
    </html> 
      

  4.   

    顺序错了,先加载option再选中。
    BindAllUserName();
    拿到选中的前面执行,而且加载option要用同步,防止在进行选中的时候,option没加载完。
      

  5.   

    看了,你的option value和内容是一样的,所以列举的情况都行,推荐用value吧
      

  6.   


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>测试用页面</title>
    <style>
    </style>
    </head>
    <body>
    <select id="sel">
    <option value="红山">红山</option>
    <option value="绿山">绿山</option>
    <option value="黑山">黑山</option>
    <option value="黄山">黄山</option>
    </select>
    <script>
    function setSelectByValue(domSelect,strVal){
    var opts = domSelect.options;
    for(var i=0,j=opts.length;i<j;i++)
    if(opts[i].value===strVal)return domSelect.selectedIndex=i;
    }
    setSelectByValue(document.getElementById('sel'),'黄山');
    </script>
    </body>
    </html>
      

  7.   

    而且你这里在生成option的时候加一个判断不就容易多了吗
    for(var i=0;i<json.depart.length;i++)
      {if(json.promanagername == json.depart[i].username){
    $("#reviewperson").append("<option value='"+json.depart[i].username+"' selected >"+json.depart[i].username+"</option>");
    }
    else{
      $("#reviewperson").append("<option value='"+json.depart[i].username+"'>"+json.depart[i].username+"</option>");
    }
      }
      

  8.   

    不用同步的话,绑定数据的操作 可以放在 加载option的回调function里面 也可以。
      

  9.   

    可能是 BindAllUserName 方法里的json数据结构可能不是你想的那样。
      

  10.   

    LZ先搞明白执行顺序,把ajax改成同步执行先
    等ajax加载完了所有option之后才能设某个option选中
    另外设某一option选中
    $("#reviewperson").val("黄山");
    不要浪费了jquery