gpt4 book ai didi

javascript - 仅当特定按钮类型导致表单提交时,如何为表单提交添加选择器

转载 作者:行者123 更新时间:2023-12-02 16:30:01 25 4
gpt4 key购买 nike

我有以下代码:

$(document).ready(function () {
$('body').on("submit", "form", function () {

$(this).find('.btn-primary').prop("disabled", "disabled");
$('#progress').show();
});
$('body').on("change", "form", function () {

$('.btn.btn-primary').prop("disabled", false);
$('#progress').hide();


});
});

但是我如何强制脚本仅在按钮具有 .btn.btn-primary 时才触发导致表单提交,而如果我使用类 .btn.btn-default 的示例按钮提交表单不触发上面的脚本?

编辑我尝试过这个,但现在脚本永远不会触发:

$(document).ready(function () {
$(document).on("submit", "form", function () {
//^^ use document instead of body
//check if button has btn.btn-default class and if exist return
if($('button').hasClass('btn.btn-default')){
return;
}
$(this).find('.btn-primary').prop("disabled", "disabled");
$('#progress').show();
});

$(document).on("submit", "form", function () {
//$('form').change(function () {
$('.btn.btn-primary').prop("disabled", false);
$('#progress').hide();


});
});

最佳答案

如果按钮具有类,则可以使用 hasClass 返回表单

$(document).on("submit", "form", function () {
//^^ use document instead of body
//check if button has btn.btn-default class and if exist return
if($('button').hasClass('btn.btn-default'){
return;
}
$(this).find('.btn-primary').prop("disabled", "disabled");
$('#progress').show();
});

关于javascript - 仅当特定按钮类型导致表单提交时,如何为表单提交添加选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389892/

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