插入和按钮禁用将所有内容放在一起

Inserting and button disable putting all together

本文关键字:在一起 按钮 插入      更新时间:2023-09-26

我有一个表的嵌套记录,我用ajax将其插入数据库的另一个表中,当我单击某个特定按钮时,值将更改为发送的数据,以此类推。我使用两个运行良好的脚本来执行此操作,一个在不刷新的情况下插入数据,另一个在单击时禁用特定按钮并将值更改为发送的数据。现在我想把它放在一起,使它成为一个整体。

插入

$(document).ready(function(){
    $("form").on('submit',function(event){ 
        event.preventDefault();
        data = $(this).serialize();
        $.ajax({
        type: "POST",
        url: "calls/insert_fryd.asp",
        data: data
        }).success(function() {

禁用按钮

$(function(){
 $(".btn-style").click(function(){
    $(this).val('data sent');
    $(this).attr('disabled', true);
  });
});

$(function(){});只是$(document).ready(function(){}); 的快捷方式

只需将这两段代码放在一个支持DOM的处理程序中即可。例如

$(function () {
    $("form").on('submit', function (event) {
        event.preventDefault();
        data = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "calls/insert_fryd.asp",
            data: data
        }).success(function () {});
    });
    $(".btn-style").click(function () {
        $(this).val('data sent');
        $(this).attr('disabled', true);
    });
});

假设".btn样式"与您的提交按钮匹配,您可以将其简化为:

$(function () {
    $("form").on('submit', function (event) {
        event.preventDefault();
        // Disable submit button on this specific form
        $('.btn-style', this).val('data sent').prop('disabled', true);
        data = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "calls/insert_fryd.asp",
            data: data
        }).success(function () {
        });
    });
});

发现的后续问题(在Chrome中不工作)归结为通过attr使用disabled。对于真正的属性(如checkeddisabled),请始终使用prop而不是attr