javascript - 将文本框数组传递给 ajax json

标签 javascript php jquery json ajax

我有两个不同的数组,即 main_name[] 和 sub_name[]。 我必须使用 AJAX 将这些值传递到下一页,以便插入到 DB 中。你能帮帮我吗,我如何将数组作为 AJAX 中的数据传递。 HTML

<input type="text" name = "main_name[]" value = "" >
<input type="text" name = "sub_name[]" value = "" >

JS

$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});

最佳答案

$('form').serialize() 将连同值一起序列化您的所有表单元素,并与 ajax 查询一起发送。

 $.ajax({
     type: "POST",
     dataType: "json",
     url: "response.php", //Relative or absolute path to response.php file
     data: $('#form_id').serialize(),
     success: function(data) {
       $(".the-return").html
     );
     alert("Form submitted successfully.\nReturned json: " + data["json"]);
    }
    });
    return false;
    });
    });

关于javascript - 将文本框数组传递给 ajax json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33228236/

相关文章:

javascript - 逗号后两位数字

javascript - 在 JavaScript 中解析类似 ISO8601 的日期 : new Date() doesn't work across browsers

javascript - 转换JSON的算法

javascript - 将内容克隆到 slider

Javascript 字符串 Var + jQuery 文本框 Val

jquery - 当元素位于其父 div 的外部(即向右滑动)时,如何隐藏它?

javascript - Popup从底部向上滑动溢出其他div block

php - Android WebView Cookie 问题

php - 基本正则表达式帮助

php - Joomla, Mysql 错误