3 回答
TA贡献1812条经验 获得超5个赞
这是我们将使用 ajax 的工作示例
<form id="sortContentImagesForm" method="PATCH" action="/">
<div id="sortContentImages">
</div>
</form>
<button id="submit-sorted-images" class="btn btn-success btn-sm"><i class="fa fa-check"></i>Submit Sorted
Images</button>
<script>
$(document).on('click', '#submit-sorted-images', function (e) {
$("#sortContentImagesForm").submit(); // Submit the form
});
$(document).on('submit', "#sortContentImagesForm", function (e) {
e.preventDefault(); // avoid to execute the actual submit of the form.
var form = $(this);
var url = form.attr('action');
var type = form.attr('method');
$.ajax({
type: type,
url: url,
data: form.serialize(), // serializes the form's elements.
success: function (payload) {
toastr.success("Updated Successfully");
},
error: function (request, status, error) {
toastr.error(request.responseText);
}
});
});
</script>
TA贡献1876条经验 获得超7个赞
您可以在不使用 JavaScript 的情况下实现您想要的。如果您form在按钮标签中设置该属性,即使它被放置在表单之外,它也会作为提交按钮工作:
<form id="refresh-form" action="test.php">
<button id="map-button">Refresh</button>
</form>
<button id="buy-button" form="refresh-form">Buy</button>
添加回答
举报