Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在列表中动态生成输入字段,然后从所述字段中检索文本?_Javascript_Ajax_Jquery - Fatal编程技术网

Javascript 如何在列表中动态生成输入字段,然后从所述字段中检索文本?

Javascript 如何在列表中动态生成输入字段,然后从所述字段中检索文本?,javascript,ajax,jquery,Javascript,Ajax,Jquery,我正在填充一个输入字段列表,以允许用户输入多个位置。(我找到了一种填充列表的方法,但我认为这可能不是正确的方法,因为我才刚刚开始,我想我最好学会正确操作)。我按如下方式填充列表: function addLocationBox(){ $('#InputBoxesList').append('<li><input type="text" /><input type="button" value ="Add Location" onclick="addLoca

我正在填充一个输入字段列表,以允许用户输入多个位置。(我找到了一种填充列表的方法,但我认为这可能不是正确的方法,因为我才刚刚开始,我想我最好学会正确操作)。我按如下方式填充列表:

function addLocationBox(){
    $('#InputBoxesList').append('<li><input type="text" /><input type="button" value ="Add Location"  onclick="addLocationBox();"/></li>')
}
函数addLocationBox(){
$(“#InputBoxesList”)。追加(“
  • ”) }
    当用户填写完字段,并在页面中稍后按下提交按钮时,我需要知道如何检索他们输入的文本。有什么建议吗?我是javascript新手,所以也欢迎使用任何其他指针:)。

    您可以使用
    .each()
    在输入之间循环

    假设您想使用Ajax和JQuery发布输入值。仅输入,而不是文本区域或选择,这对于示例来说更容易

    首先,需要文本输入和字符串变量来存储数据。 在这个字符串中,我通常首先存储submit的
    值。它在服务器端很有用

    var $inputs    = $("input").not("input[type=submit]"),
        submitval  = $("input[type=submit]").val(),
        datastring = "submit="+submitval;
    
    然后使用
    .each()
    循环输入并在数据字符串中附加名称/值:

    $inputs.each(function() {
      var $this = $(this),
          name  = $this.attr("name"),
          val   = $this.val();
    
          datastring += "&"+name+"="+val;    
    });
    
    现在,您有了一个数据字符串,可以像这样发布到form.php中,每个示例如下:

    $.ajax({
      type: "POST",
      data: datastring,
      url: "form.php",
      success: function(data) {
        // your success handler
      }, 
      error: function() {
        // your error handler
      }   
    });
    
    这不是一种剪切粘贴的解决方案,而是一种通用的方法

    它不会处理文本区域、复选框、选择等。
    它不会处理多个表单。
    它不会在发布数据之前检查数据

    因此请谨慎使用。

    您可以使用
    .each()
    在输入之间循环

    假设您想使用Ajax和JQuery发布输入值。仅输入,而不是文本区域或选择,这对于示例来说更容易

    首先,需要文本输入和字符串变量来存储数据。 在这个字符串中,我通常首先存储submit的
    值。它在服务器端很有用

    var $inputs    = $("input").not("input[type=submit]"),
        submitval  = $("input[type=submit]").val(),
        datastring = "submit="+submitval;
    
    然后使用
    .each()
    循环输入并在数据字符串中附加名称/值:

    $inputs.each(function() {
      var $this = $(this),
          name  = $this.attr("name"),
          val   = $this.val();
    
          datastring += "&"+name+"="+val;    
    });
    
    现在,您有了一个数据字符串,可以像这样发布到form.php中,每个示例如下:

    $.ajax({
      type: "POST",
      data: datastring,
      url: "form.php",
      success: function(data) {
        // your success handler
      }, 
      error: function() {
        // your error handler
      }   
    });
    
    这不是一种剪切粘贴的解决方案,而是一种通用的方法

    它不会处理文本区域、复选框、选择等。
    它不会处理多个表单。
    它不会在发布数据之前检查数据

    所以请谨慎使用。

    只需选择
    $(“#InputBoxesList input”)
    并循环它们即可获得每个文件的文本。只需选择
    $(“#InputBoxesList input”)
    并循环它们即可获得每个文件的文本。