简体   繁体   English

JQuery.val()不起作用

[英]JQuery.val( ) not working

I am trying to simply get the value of an input, and attach it to a 'zipcode' variable. 我试图简单地获取输入的值,并将其附加到“邮政编码”变量。 However whenever I try to console the value, it just comes up as blank. 但是,每当我尝试控制该值时,它就变成空白。

$(document).ready(function(){
  var zipcode = $('.zipcode-search').val()

  $('#button').on('click', function(){
    console.log(zipcode)
  })

HTML 的HTML

<div class="col-sm-12">
    <div class="text-center">
        <input class="text-center zipcode-search" type="text" name="zipcode-search" placeholder="Enter your city"/> 
        <button id="button" class="btn btn-success">
          Submit
        </button>

I am doing this on codepen.io. 我正在codepen.io上执行此操作。 I'm not sure if that would have anything to do with it but I thought I might add that. 我不确定这是否与它有关,但我想我可以补充一点。 Is there any other way besides .val() to get the value of an element ? 除了.val()之外,还有其他方法可以获取元素的值吗?

I'm assuming you want to update zipcode everytime the button is clicked. 我假设您想在每次单击按钮时更新zipcode You should move that logic to be inside of your event handler. 您应该将该逻辑移到事件处理程序中。

$(document).ready(function(){

  $('#button').on('click', function(){
    var zipcode = $('.zipcode-search').val()
    console.log(zipcode)
  })

.zipcode-search , doesn't have a value when the page loads, and your variable's value is only assigned once, when the page loads. .zipcode-search ,在页面加载时没有值,并且在页面加载时,变量的值仅分配一次。

If you want to update your variable whenever your button is clicked, you'll need to move your call to $.val , into your click event handler. 如果要在单击按钮时更新变量,则需要将对$.val的调用移至click事件处理程序中。

Here's an example: 这是一个例子:

$(document).ready(function() {
    var zipcode = null;

    $('#button').on('click', function() {
        zipcode = $(".zipcode-search").val();
        console.log(zipcode);
    });
});

Note how I assign zipcode a value of null when the page loads, I did this because the chances of .zipcode-search having a value when the page loads, are slim-to-none without more advanced logic. 请注意,我是如何在页面加载时为zipcode分配null的,之所以这样做,是因为在没有页面更高级逻辑的情况下, .zipcode-search在页面加载时具有值的可能性.zipcode-search

Edit : Examples provided in other answers, have been redeclaring the zipcode variable every time your button's click event is fired. 编辑 :其他答案中提供的示例在每次触发按钮的click事件时都重新声明了zipcode变量。 This is not needed. 这是没有必要的。

Declaring/redeclaring the variable every time the click event is fired, makes that variable exclusive to the scope of your callback function for the click event, which will prevent you from being able to reuse the variable outside of that callback. 每次触发click事件时都要声明/重新声明该变量,从而使该变量不属于click事件的回调函数的作用域,这将使您无法在该回调之外重用该变量。

You should get the value inside the onClick function: 您应该在onClick函数中获取值:

$(document).ready(function(){
    $('#button').on('click', function(){
       var zipcode = $('.zipcode-search').val()
       console.log(zipcode)
    });
});

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

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