简体   繁体   English

javascript搜索功能不起作用

[英]javascript search function not working

I have a search form which which takes an input and pass the data to a django view. 我有一个搜索表单,该表单接受输入并将数据传递到django视图。

the form has a search button which on click opens a input box like shown in images given below: 表单上有一个搜索按钮,单击该按钮会打开一个输入框,如下图所示:

在此处输入图片说明 在此处输入图片说明

Now when i enter something into the input and press enter, it just collapses the input box and no action occurs. 现在,当我在输入中输入内容并按Enter键时,它只会折叠输入框,并且不会发生任何动作。 It happens every time. 每次都会发生。 I want it to call the function associated with form. 我希望它调用与表单关联的函数。
I figured out that the problem is in the javascript but don't know how to fix it. 我发现问题出在javascript中,但不知道如何解决。

html: 的HTML:

<form class="navbar-form" role="search" method="POST" action="{% url 'search' %}">
{% csrf_token %}
    <div class="input-group">
        <input type="text" class="form-control" placeholder="Search" name="search_box">
        <span class="input-group-btn">
            <button name="search" type="submit" class="search-btn"> <span class="glyphicon glyphicon-search"> <span class="sr-only">Search</span> </span> </button>
        </span>
    </div>
</form>

javascript: javascript:

e(function() {
            function t() {
                var t = e('.navbar-collapse form[role="search"].active');
                t.find("input").val(""), t.removeClass("active")
            }
            e('header, .navbar-collapse form[role="search"] button[type="reset"]').on("click keyup", function(n) {
                console.log(n.currentTarget), (27 == n.which && e('.navbar-collapse form[role="search"]').hasClass("active") || "reset" == e(n.currentTarget).attr("type")) && t()
            }), e(document).on("click", '.navbar-collapse form[role="search"]:not(.active) button[type="submit"]', function(t) {
                t.preventDefault();
                var n = e(this).closest("form"),
                    i = n.find("input");
                n.addClass("active"), i.focus()
            }), e(document).on("click", '.navbar-collapse form[role="search"].active button[type="submit"]', function(n) {
                n.preventDefault();
                var i = e(this).closest("form"),
                    s = i.find("input");
                e("#showSearchTerm").text(s.val()), t()
            })
        }

css: CSS:

.navbar-collapse form[role="search"] input {
    font-size: 18pt;
    opacity: 0;
    display: none;
    height: 48px;
    position: relative;
    z-index: 2;
    float: left;
}

.navbar-collapse form[role="search"].active input {
    display: table-cell;
    opacity: 1;
    z-index: 100;
    border-radius: 0;
    border: none;
    line-height: 45px;
    height: 75px;
    font-size: 14px;
    padding: 0px 25px;
    width: 315px;
}

.navbar-collapse {float:right; padding:0px}

its because you didn't submit form Try this to submit form.. 其原因是您未提交表单尝试提交表单。

Without AJAX 没有AJAX

$('form#myForm').submit();

with performing AJAX 与执行AJAX

$('input#submitButton').click( function() {
    $.post( 'some-url', $('form#myForm').serialize(), function(data) {
         ... do something with response from server
       },
       'json' // I expect a JSON response
    );
});

$('input#submitButton').click( function() {
    $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
                   ... do something with the data...
                 }
    });
});

Hope this helps.. 希望这可以帮助..

Letting you know the step by step procedure. 让您知道分步过程。

  1. Set a .keyup event on the search input. 在搜索输入上设置一个.keyup事件。 $("[name='search_box']").keyup(function(e){ .... });
  2. Check if the keycode of the pressed key is equal to 13 (enter key) if(e.keyCode==13){ ... } 检查所按下键的键码是否等于13(输入键) if(e.keyCode==13){ ... }
  3. If it is 13 then call the function associated with the form. 如果是13,则调用与表单关联的函数。

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

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