本帖最后由 niunan 于 2010-06-17 13:57:47 编辑

解决方案 »

  1.   

    我估计应该是onkeypress="responseEnter(event);"的问题
      

  2.   


        <form id="form1" runat="server">
        <input id="key" type="text" onkeypress="responseEnter(serder, event);" />
        <button style="cursor: pointer;" onclick="search();" type="button">
            搜索
        </button>
        </form>
        <script type="text/javascript">
            document.getElementById("key").focus(); //无效        
        </script>
      

  3.   

    汗。  document.getElementById("key").focus(); //无效
                    alert("请输入搜索关键字!");
    换一下顺序不就可以了么?
      

  4.   

    当你点击按钮或按回车时,页面刷新了,当focus执行后,页面刷新,所以无效,放在最下面的话,应该就可以了
      

  5.   

    我也觉得是把
    document.getElementById("key").focus(); //无效
      alert("请输入搜索关键字!");
    这俩句换个位置就可以了
      

  6.   

              document.getElementById("key").focus(); //无效
                    alert("请输入搜索关键字!");前后位置换一下就OK了!如果alert()在后面,那么前面刚获取焦点,后面警告框就又获取了。
      

  7.   

    这样当然无效了,alert焦点就跳到了对话框,你之前的focus当然没用了
      

  8.   

    document.getElementById("key").focus(); //无效
      alert("请输入搜索关键字!");你怎么让用户获得焦点就提示,请输入搜索关键字呢,
    你应该在<input id="key" type="text" onkeypress="responseEnter(event);" />
    旁放个<div>请输入搜索关键字</div>
    然后用户在点回车或者失去焦点的时候,把div里的innerHTML="";换成你要提示的消息!~
      

  9.   

    <head runat="server">
        <title></title>    <script type="text/javascript">
            function responseEnter(e) {
                // 响应回车
                var key = window.event ? e.keyCode : e.which;
                if (key == 13) {
                    //alert("回车了");
                    return search();
                }
            }
            function search() {
                // 搜索
                var key = document.getElementById("key").value;
                if (key.length == 0) {                
                    document.getElementById("key").focus(); //无效
                    alert("请输入搜索关键字!");
    return false;
                } else {
                    var url = "SearchItem.aspx?key=" + encodeURI(key) + "&typeid=0&typename=所有分类";
                    window.open(url);
                }
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <input id="key" type="text" onkeypress="return responseEnter(event);" />
        <button style="cursor: pointer;" onclick="return search();" type="button">
            搜索
        </button>
        </form>
    </body>
    </html>
      

  10.   

      function search() {
                // 搜索
                var key = document.getElementById("key").value;
                if (key.length == 0) {   
                   window.setTimeout(
    function(){
                    document.getElementById("key").focus()},500); 
    //无效
                    alert("请输入搜索关键字!");
                } else {
                    var url = "SearchItem.aspx?key=" + encodeURI(key) + "&typeid=0&typename=所有分类";
                    window.open(url);
                }
            }
    注意:给我加分啊