修复我的javascript搜索栏

时间:2013-05-04 03:43:16

标签: javascript html searchbar

所以我编写了我的搜索栏,我不知道为什么它不起作用。我想要当用户键入“论坛”时它会显示一条消息,当他们点击它时它会将它们发送到论坛,但我正在测试它是否会显示警告而我无法说出任何内容。

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>website name / slogan</title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.min.css" />

</head>

<body>
<div class="row">
    <div class="span12">
        <form method="get" action="/" class="form-inline" >
            <input name="q" class="span5" id="searchBar" type="text" placeholder="Click below on how to use" />
            <button type="submit" id="submit" class="btn btn-primary"> <i class="icon-search icon-white"></i></button>
        </form>
    </div>
</div>

<script type="text/javascript" src="js/bootstrap.js"></script>
</body>

的Javascript

function myNavSystem() {
    var mySearchBar = document.getElementById('searchBar');
    if(mySearchBar = "forums")
        document.getElementById('submit');
        alert("you have tried accessing forums")
    else
        alert("You have entered invalid text, try again.")
}

1 个答案:

答案 0 :(得分:2)

一个明显的错误是你在if / else语句(和分号)中忘了花括号。

if(mySearchBar.value == "forums") {
    document.getElementById('submit');
    alert("you have tried accessing forums");
} else {
    alert("You have entered invalid text, try again.");
}