简体   繁体   English

从具有相同类的输入字段中获取所有值,然后通过AJAX调用发送到脚本

[英]Obtain All Values from Input Fields Having Same Class and Send to a Script via an AJAX Call

I have a form with the following input fields: 我有一个包含以下输入字段的表单:

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

Then I have the following JS function: 然后,我有以下JS函数:

function showHint()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
  //GET JSON from Validation.php and extract the nodes
  var response = xmlhttp.responseText;
  var parseJson = JSON.parse(response);
  var resultCode = parseJson.code;
  var resultMessage = parseJson.message;

    var DoB = [];

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

    var newDob = DoB.slice(0,-1);

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

But I'm getting the following error: 但我收到以下错误:

myfile.js:352 Uncaught ReferenceError: newDob is not defined
    at showHint (myfile.js:352)
    at HTMLInputElement.onkeyup (VM3224 :618)
showHint    @   myfile.js:352
onkeyup @   VM3224 :618

What I'm trying to do is to obtain all the fields of class 'date' and send them - delimited to an external script. 我想做的是获取类'date'的所有字段并将其发送-分隔到一个外部脚本。

try to use jQuery built in serializer 尝试使用序列化器内置的jQuery

 console.log($('form').serialize()) // in your submit function 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> <input type="text" class="date" name="date[]" onkeyup="showHint()" /> </form> 

regarding you method you have scoping issue check the following code 关于您的方法有范围问题,请检查以下代码

function showHint()
{

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//GET JSON from Validation.php and extract the nodes
var response = xmlhttp.responseText;
var parseJson = JSON.parse(response);
var resultCode = parseJson.code;
var resultMessage = parseJson.message;



 }
}
var DoB = [];

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

 var newDob = DoB.slice(0,-1);
 xmlhttp.open("GET","/validation.php?q="+newDob,true);
 xmlhttp.send();
} 

you can't define a variable inside onreadystatechange event because it will not be available in the moment you call open method 您无法在onreadystatechange事件中定义变量,因为在您调用open方法时该变量将不可用

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

相关问题 如何通过Ajax在php上发送多个已创建输入字段的值 - How to send the values of multiple created input fields on php via ajax 如何通过Ajax发送具有相同类名或ID的多个输入值 - How to send multiple input values with same class name or id via ajax 1个脚本中来自多个输入字段的jQuery AJAX调用 - jQuery AJAX call from multiple input fields within the 1 script 如何将具有相同类和名称的多个输入字段传递给ajax请求并根据回复更改相应的字段? - How to pass multiple input fields having same class and name to ajax request and change corresponding fields based on reply? 如何通过Ajax POST传递多个具有相同名称的输入数组元素值,例如ex:sikll [] - How to pass Multiple input array element values having same name like ex: sikll[] via Ajax POST 从具有相同类名的不同输入字段获取值 - Getting values from different input fields with same class name 通过AJAX调用填写具有相同名称的动态表单字段 - Fill out dynamic form fields with the same name via AJAX call Laravel Ajax Input :: all()通过FormData发送时返回空 - Laravel Ajax Input::all() return empty when send it via FormData 如何获取输入字段值的数组并将其通过Ajax调用传递 - How to take an array of input fields values and pass it through an Ajax call 通过Ajax发送相同名称的多个复选框值 - Send same name multiple checkbox values via ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM