繁体   English   中英

Jquery:如何在不使用“更改”、“keyup”等交互的情况下使用现有的输入文本值

[英]Jquery: how to use the existing input text value without using on interaction like "change", "keyup"

这是用于显示输出的 jquery 代码。但我在输入文本中使用现有值。 因此,数据可以显示,但我需要单击文本框并在文本框内更改以显示数据库中的项目。

我不想更改文本框值。

$(document).ready(function(){
    $('.search-box2 input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal2 = $(this).val();
        var resultDropdown2 = $(this).siblings(".result2");
        if(inputVal2.length){
            $.get("ajax2.php", {term: inputVal2}).done(function(data){
                // Display the returned data in browser
                resultDropdown2.html(data);
            });
        } else{
            resultDropdown2.empty();
        }
    });
$(document).ready(function(){
     const val = $('.search-box2 input[type="text"]').val();
     $.get("ajax2.php", {term: val}).done(function(data){
                // Display the returned data in browser
                resultDropdown2.html(data);
          });
 });

只需随时查询输入并通过value属性获取其值:

// VanillaJS™ variant
const value = document.querySelector('.search-box2 input[type="text"]').value;

// jQuery variant
var value = $('.search-box2 input[type="text"]').val();
$(document).ready(function() {
  yourFunctionName(); /* calling function at first time */

  $('.search-box2 input[type="text"]').on("keyup input", yourFunctionName);
  // keyup and input will call the same function as well.

  function yourFunctionName() {
    //...   
  }
});

注意: yourFunctionName它只是一个包含函数代码的变量。 yourFunctionName() - 带括号 = 表示“运行函数”。

暂无
暂无

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

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