javascript - 使用具有 PHP 功能的表单提交后禁用按钮。没有 jQuery

标签 javascript php html

我有一个在 PHP 中回显的 HTML 表单,该表单操作调用一个 PHP 函数。如果可能的话,我想在单击提交按钮后禁用它,而不使用 jQuery。

#PHP
echo '
<form method="POST" action="' . phpFunction() . '">
    //form fields
    <button name="submit" type="submit" class="btn btn-primary btn-block">Enviar</button>
</form>';

我尝试在按钮上使用onclick="this.disabled=true",但它阻止了 PHP 函数的执行。 我应该怎么做才能执行该功能并禁用该按钮?

最佳答案

使用这个简单的代码禁用提交按钮

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        function form_submission(form_id, button_id) {
            $('#btn_add').attr('disabled', 'disabled');
            document.getElementById(button_id).style.display = "none";
            document.forms[form_id].submit();
        }
    </script>
</head>
<body>
    <form action="submit.php" method="post" name="form_name" id="form_name">
        <input type="text" name="first_name" />
        <input type="text" name="last_name" />
        <input type="button" name="btn_add" id="btn_add" value="save" onclick="form_submission('form_name', 'btn_add')" />
    </form>
</body>

关于javascript - 使用具有 PHP 功能的表单提交后禁用按钮。没有 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59081490/

相关文章:

javascript - jQuery 对话框不关闭

php - symfony 2.3,使用外键持久化相关实体

html - CSS内联不起作用

javascript - Sails.js 模型 updatedAt (updated_at) 不自动更新

javascript - 如何在 raphael.js 文档中添加子 svg 标签?

php - 取消设置多个数组元素的更好方法

html - 溢出 : scroll; only the y-axis whilst keeping x-axis visible

javascript - 防止窗口向下滚动

javascript addClass 似乎没有触发我的类的 css

php - 在 csv_from_result 方法中为我们将 mysql 时间戳转换为 codeigniter 查询对象中的日期