因此,我正在对搜索栏进行编程,我不知道为什么它不起作用。我想在用户键入“论坛”时说出一条消息,然后单击“确定”时将其发送到论坛,但是我正在测试以查看它是否会显示警报,而我却无话可说。

的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>




Java脚本

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.")
}

最佳答案

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

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

关于javascript - 修复我的JavaScript搜索栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16370357/

10-12 12:35
查看更多