简体   繁体   English

在键盘上获取每个输入值到PHP脚本

[英]On keyup get each input values to php script

I'm trying to get each input value on keyup with comma separated. 我正在尝试使用逗号分隔键输入每个输入值。 My code is working fine with onclick event but not with on keyup. 我的代码在onclick事件上运行正常,但在keyup上运行良好。

<input type="text" class="date" name="date[]" onkeyup="showHint(this.value)" />

This is part of my js function showhint where i'm defining the input value. 这是我的js函数showhint的一部分,我在其中定义输入值。

var DoB = [];
$(".date").each(function(){
     DoB.push($(this).val());
});
var newDob = DoB.slice(0,-1);

xmlhttp.open("GET","validation.php?q="+newDob+",true);

Can anyone help me with this what is my mistake here? 谁能帮助我,这是我的错误?

Thanks in advance. 提前致谢。

try this: 尝试这个:

$( ".date" ).keyup(function() {
   DoB.push($(this).val());
});

As per your code, you don't have to keep array DoB. 根据您的代码,您不必保留数组DoB。 as you have only one dob element. 因为您只有一个dob元素。 and you can directly get value of input. 您可以直接获取输入值。

following code will work for you 以下代码将为您工作

function showHint(){
    var DoB = $(".date").val();
    console.log(DoB);
    xmlhttp.open("GET","validation.php?q="+DoB +",true);
}

Are you sure the issue is the keyup ? 你确定这个问题是keyup It seems to me the DoB.slice(0, -1) is causing your code not to work. 在我看来DoB.slice(0, -1)导致您的代码无法正常工作。

I replaced it with a DoB.join(','); 我将其替换为DoB.join(','); to create the comma separated string. 创建逗号分隔的字符串。

 function showHint(someValue) { var DoB = []; $(".date").each(function(){ //console.log($(this).val()); DoB.push($(this).val()); }); //var newDob = DoB.slice(0, -1); var newDob = DoB.join(','); document.getElementById("URL").value = "validation.php?q="+newDob; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="date" name="date[]" onkeyup="showHint(this.value)" /> <input type="text" class="date" name="date[]" onkeyup="showHint(this.value)" /> <br>URL : <input type="text" size="50" id="URL"/> 

I also had this happen to me before, when I used the onkeyup event in the script itself it works fine for me. 之前我也遇到过这种情况,当我在脚本本身中使用onkeyup事件时,它对我来说很好用。

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

 document.getElementById("fname").onkeyup = function() {myFunction()}; function myFunction() { var x = document.getElementById("fname"); x.value = x.value.toUpperCase(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!DOCTYPE html> <html> <body> <p>Press a key inside the text field and release it to set the text to uppercase.</p> Enter your name: <input type="text" id="fname"> </body> </html> 

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

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