繁体   English   中英

如何检索keyup上的输入值?

[英]How to retrieve the value of an input on keyup?

我想要实现的是输出实际值/文本,以便我可以将其保存到变量中,但是控制台给了我一个数字指示器,并且每次我键入时它都会不断添加它。

<input id="usp-custom-3" type="text">
var country1 = $("#usp-custom-3").html();
$("#usp-custom-3").on("keyup", function() { 
    console.log(country1);
});

您的代码存在两个问题。 首先你需要得到inputval() ,而不是它的html() 其次,每次事件发生时都需要检索值,所以将它放在事件处理程序中,如下所示:

 $("#usp-custom-3").on("keyup", function() { var country1 = $("#usp-custom-3").val(); console.log(country1); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="usp-custom-3" type="text"> 

暂无
暂无

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

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