Javascript Jquery:如何使用现有的输入文本值,而不使用诸如;更改“&引用;keyup“;
以下是用于显示输出的jquery代码。。但我在输入文本中使用现有值。 因此,数据可以显示,但我需要单击文本框并在文本框内更改以显示数据库中的项目 我不想更改文本框的值Javascript Jquery:如何使用现有的输入文本值,而不使用诸如;更改“&引用;keyup“;,javascript,jquery,html,Javascript,Jquery,Html,以下是用于显示输出的jquery代码。。但我在输入文本中使用现有值。 因此,数据可以显示,但我需要单击文本框并在文本框内更改以显示数据库中的项目 我不想更改文本框的值 $(document).ready(function(){ $('.search-box2 input[type="text"]').on("keyup input", function(){ /* Get input value on change */ var inputVal2 = $
$(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();
}
});
只要随时查询输入,并通过
value
属性获取其值:
//VanillaJS™ 变体
常量值=document.querySelector('.search-box2输入[type=“text”]')。值;
//jQuery变量
var值=$('.search-box2输入[type=“text”]”)。val();
注意:yourFunctionName
-它只是一个变量,包含函数代码yourFunctionName()
-带括号=表示“运行函数”。让X=$(inputSelector).val()代码>$('.search-box2输入[type=“text”]”)。triggerHandler('input')代码>
$(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() {
//...
}
});
$(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);
});
});