gpt4 book ai didi

php - 添加新表单时不要接触 jQuery

转载 作者:行者123 更新时间:2023-11-28 04:04:19 25 4
gpt4 key购买 nike

假设我有一个输入:<input type="text" id="smth" value="" />我有 jQuery 将此数据传递给 .php 文件

$("#submit_button").click(function() {
var info = {};
info['smth'] = $('#smth').val();
$.ajax({
type: "POST",
url: "insert.php",
data: info,
success: function(msg){
alert("Well done!");
}
})
})

效果很好,但假设我需要添加新的输入,假设:

<input type="text" id="smth2" value="" /> ,

所以我需要在 jQuery 中在 info['smth'] 之后添加这一行:

info['smth2'] = $('#smth2').val(); .

我的问题是,如何避免这种情况?每次添加新输入时都可以不在 jQuery 中添加新行吗?它会是什么样子?谢谢。

最佳答案

希望您添加所有的 <input> s 到 <form> .希望所有这些<input>我们有name属性(例如你的第一个应该有 name="smth" )。这样的话,问题就给你解决了!

var info = $('#myForm').serialize();

完成!

关于php - 添加新表单时不要接触 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5974943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com