gpt4 book ai didi

javascript - 克隆表单的两个提交按钮

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:01 24 4
gpt4 key购买 nike

有一个 NEWFORM 按钮可以在点击时创建新的表单。每个表单有两个按钮。这两个按钮用作提交。并且有两个不同的操作请求页面。我的问题:是否可以将这两个按钮用作两个提交?这是我的片段:

$(document).ready(function() {
$(".newform").click(function() {
$(".MyForm")
.eq(0)
.clone()
.show()
.insertAfter(".MyForm:last");
});

$(document).on('click', '.MyForm button[type=submit]', function(e) {
e.preventDefault() // To make sure the form is not submitted
var $frm = $(this).closest('.MyForm');
console.log($frm.serialize());
$.ajax(
$frm.attr('action'),
{
method: $frm.attr('method'),
data: $frm.serialize()
}
);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<span class="newform">NEWFORM+</span>
<div class="all">
<form class="MyForm" method="post" action="secondpage.htm">
<input type="text" placeholder="name" value="Aynaz" name="a1" />
<select name="Avg">
<option value="1">1</option>
<option value="2">2</option>
</select>
<button type="submit">add from DB</button>
<button type="submit">Remove from DB</button>
</form>
</div>

最佳答案

在这里试试我为每个按钮元素创建了两个函数并向每个类添加类以绑定(bind) jquery 点击

 $(document).ready(function() {
$(".newform").click(function() {
$(".MyForm")
.eq(0)
.clone()
.show()
.insertAfter(".MyForm:last");
});
//click handler for add from DB
$('body').on('click', '.addfromdb', function(e) {
e.preventDefault() // To make sure the form is not submitted
var $frm = $(this).closest('.MyForm');
console.log($frm.serialize());
$.ajax(
$frm .attr('action'),
{
method: $frm.attr('method'),
data: $frm.serialize()
}
);
});
//click handler for remove from DB
$('body').on('click', '.removefromdb', function(e) {
e.preventDefault() // To make sure the form is not submitted
var $frm = $(this).closest('.MyForm');
console.log($frm.serialize());
$.ajax(
$frm .attr('action'),
{
method: $frm.attr('method'),
data: $frm.serialize()
}
);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<span class="newform">NEWFORM+</span>
<div class="all">
<form class="MyForm" method="post" action="secondpage.htm">
<input type="text" placeholder="name" value="Aynaz" name="a1" />
<select name="Avg">
<option value="1">1</option>
<option value="2">2</option>
</select>
<button type="submit" class="addfromdb">add from DB</button>
<button type="submit" class="removefromdb">Remove from DB</button>
</form>
</div>

已更新

 $(document).ready(function() {
$(".newform").click(function() {
$(".MyForm")
.eq(0)
.clone()
.show()
.insertAfter(".MyForm:last");
});
//click handler for add from DB
$('body').on('click', '.addfromdb', function(e) {
e.preventDefault() // To make sure the form is not submitted
var $frm = $(this).closest('.MyForm');
console.log($frm.serialize());
$.ajax(
$(this).attr('formaction'),
{
method: $frm.attr('method'),
data: $frm.serialize()
}
);
});
//click handler for remove from DB
$('body').on('click', '.removefromdb', function(e) {
e.preventDefault() // To make sure the form is not submitted
var $frm = $(this).closest('.MyForm');
console.log($frm.serialize());
$.ajax(
$(this).attr('formaction'),
{
method: $frm.attr('method'),
data: $frm.serialize()
}
);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<span class="newform">NEWFORM+</span>
<div class="all">
<form class="MyForm" method="post">
<input type="text" placeholder="name" value="Aynaz" name="a1" />
<select name="Avg">
<option value="1">1</option>
<option value="2">2</option>
</select>
<button type="submit" formaction="first.htm"class="addfromdb">add from DB</button>
<button type="submit" formaction="secondpage.htm" class="removefromdb">Remove from DB</button>
</form>
</div>

关于javascript - 克隆表单的两个提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327964/

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