jQuery代码不会连接到搜索表单 [英] jQuery Code won't connect to search form

查看:94
本文介绍了jQuery代码不会连接到搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题适用于这个问题/帖子
我有一个搜索表单,要求:
A:从搜索表单中提取用户输入,以便我们的后端可以为用户提供相关的搜索结果。
B:我们需要能够在一个或两个搜索字段中插入默认输入,以增强用户搜索体验。这应该替换任何占位符值。

This question pertains to this question/post. I have a search form that requires: A: To pull the user input from the search form so our backend can provide the user with relevant search results. B: We need to be able to insert default input into either, or both, search fields to enhance the user search experience. This should replace any placeholder values.

我在main.js文件中尝试了以下代码(链接到testpage.html):

I have tried the following code in my main.js file (linked to the testpage.html):

$(document).ready(function(){
  $("#btn-search").click(function(e){
    e.preventDefault();
    var x = $("#keyword").val("Nurse");
    var y = $("#location").val();
    $("#keyword").attr("value", x);
    $("#location").attr("value", y);
  });
});

我也试过以下内容:

$(document).ready(function(){
  $("#btn-search").click(function(e){
    e.preventDefault();
    $("#keyword").val(Nurse);
    $("#location").val();
  });
});

你知道为什么这不起作用吗?我甚至试图把它放在页面正文中:

Do you know why this wouldn't work? I even tried to put this in the body of the page:

<script>
  $("#btn-search").click(function(e){
    e.preventDefault();
    $("#keyword").val(Nurse);
    $("#location").val();
  });
</script>

这是html表格:

<form class="form-inline no-margin center-block" action="http://www.somewebsite.com/unknown" method="POST" role="form">
                  <div class="row">
                    <center>
                    <fieldset>

                      <!-- Search input-->
                      <div class="form-group center-block">
                        <div class="col-sm-4">
                          <label class="sr-only" for="keyword">From:</label>
                          <input id="keyword" name="keyword" type="search" placeholder="Keyword" value="" class="form-control input-lg">

                        </div>
                      </div>

                      <!-- Search input-->
                      <div class="form-group center-block">
                        <div class="col-sm-4">
                          <label class="sr-only" for="location">From:</label>
                          <input id="location" name="location" type="search" placeholder="City, State, or Zip Code" class="form-control input-lg">

                        </div>
                      </div>

                      <input type="hidden" name="unknown" value="unknown">

                      <!-- Button -->
                      <div class="form-group center-block">
                        <div class="col-sm-2">
                          <button type="submit" id="btn-search" name="btn-search" class="btn btn-primary btn-lg btn-tusaj">Search</button>
                        </div>
                      </div>
                    </fieldset>
                  </div>
                </form>


推荐答案

$(document).ready(function(){
  $("#btn-search").click(function(e){
    e.preventDefault();
    $("#keyword").val("Nurse");
    $("#location").val("");
  });
});

val()需要字符串。因此,使用字符串可以解决问题。

val() requires a String. So using a string should solve the problem.

参考: http://api.jquery.com/val/

这篇关于jQuery代码不会连接到搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆