简体   繁体   English

用户未在搜索框中输入任何内容时不执行任何操作

[英]Do nothing when user does not input anything in the search box

How can I make the page do nothing when user does not input anything in the search box? 当用户未在搜索框中输入任何内容时,如何使页面不执行任何操作?

When it does not have anything in the search box, I want the page to remain the same. 当搜索框中没有任何内容时,我希望页面保持不变。

How can I do this? 我怎样才能做到这一点?

This is my current code: 这是我当前的代码:

<html>
    <head>
        <title>
            Brandon's Search Engine
        </title>
    </head>
    <script type="text/javascript">

        //document.getElementById("suggestion")

        function getSuggestion(q) {
            var ajax;
            if(window.XMLHttpRequest)//for ie7+, FF, Chrome
                ajax = new XMLHttpRequest();//ajax object
            else
                ajax = new ActiveXObject("Microsoft.XMLHTTP");//for ie6 and previous
            ajax.onreadystatechange = function() {
                if(ajax.status === 200 && ajax.readyState === 4) {
                    //if result are not there then don't display them
                    if(ajax.responseText === "")
                        document.getElementById("suggestion").style.visibility = "hidden";
                    else {
                        document.getElementById("suggestion").style.visibility = "visible";
                        document.getElementById("suggestion").innerHTML = ajax.responseText;
                    }
                }
            };
            ajax.open("GET", "suggestion.php?q=" + q, false);
            ajax.send();
        }
    </script>
    <body>
        <form method="GET" action="search.php" name="q">
            <table align="center">
                <tr>
                    <td>
                        <h1><center>Brandon's Search Engine</center></h1>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <input type="text" name="q" style="height: 27px; width: 650px; padding: 2px"
                               onkeyup="getSuggestion(this.value)" autocomplete="off" onblur="blur() document.getElementById('suggestion').style.visibility = 'hidden'"/>

                        <div id="suggestion" style="width: 648px">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <input type="submit" name="submit" value="Search" style="height: auto; width: 60px; padding: 2px" />
                        <input type="reset" value="Clear" style="height: auto; width: 50px; padding: 2px" />
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        To insert your site in result fill in the form at <a href="insert.php">here</a>.
                    </td>
                </tr>
            </table>
            <input type="hidden" name="page" value="0" />
        </form>
    </body>
</html>

You should validate your form, like: 您应该验证您的表单,例如:

<form method="GET" action="search.php" name="q" onsubmit="return validateForm()">

Your function will be: 您的功能将是:

function validateForm() {
 if(document.getElementById('q').value=='') return false;
 return true;
}

You can do it on the form submit function like: 您可以在表单提交功能上执行此操作,例如:

Your form will be like: 您的表格将如下所示:

<form method="GET" action="search.php" name="q" id="searchForm">

And the script will be like: 脚本将类似于:

$("#searchForm").submit(function(e) {
if($("#q").val() == '')
{
    e.preventDefault();
}
});

By the way this solution is using JQuery. 顺便说一下,此解决方案正在使用JQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM