gpt4 book ai didi

javascript - 提交按钮不会调用该操作

转载 作者:行者123 更新时间:2023-12-02 17:15:36 25 4
gpt4 key购买 nike

我使用以下按钮,该按钮工作正常并按预期调用操作,

保存按钮

 @using (Html.BeginForm("edit", "user", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="actionbtn" value="Save" name="buttonType" />

</div>
</div>
}

检查按钮

@using (Html.BeginForm("Check", "User"))
{
<input type="submit" id="btnConnect" value="Check" />
<span id='result'></span>
}

现在,当我添加以下代码(如果操作成功与否,应该添加一些文本)时,保存按钮不会调用该操作,我在这里做错了什么?

$("form").submit(function (e) {
// Cancel the default submission
e.preventDefault();

// Perform the AJAX call
var target = $(this).attr('action');
$.post(target, function(result) {
// Check the value of result
if (result === "True") {
// It was successful, make result text green.
$("#result").css('color', 'green').html("successful.");
} else {
// Otherwise, it failed, display as red.
$("#result").css('color', 'red').html("Failed");
}

});
});

我尝试删除 e.preventDefault();没有成功...

最佳答案

您需要像这样检查通过哪个按钮提交的表单。

你必须这样做来限制它:

$("form").submit(function (e) {
// Cancel the default submission
e.preventDefault();

if($(this).find('input[type="submit"]').val() === "Check") // form submitted via Check button
{
// Perform the AJAX call
var target = $(this).attr('action');
$.post(target, function(result) {
// Check the value of result
if (result === "True") {
// It was successful, make result text green.
$("#result").css('color', 'green').html("successful.");
} else {
// Otherwise, it failed, display as red.
$("#result").css('color', 'red').html("Failed");
}

});
}
else
{
// form submitted from Save button
}
});

关于javascript - 提交按钮不会调用该操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476616/

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