将数据保存到数据库(ajax/php)后,使文本字段变灰/禁用

Making textfield greyed out/disabled after saving data to database (ajax/php)

本文关键字:文本 字段 禁用 php 保存 数据 数据库 ajax      更新时间:2023-09-26

我有以下代码:

<input type="text" value="name" id="name" />
<input type="button" id="button" value="Click Me" /></p>
<script>
    $( "#button" ).click(function() {
        var text = $("#name").val();
        $.ajax({
            url: 'page.php',
            type: "POST",
            data: {
                text: text
            },
            success: function(response) {
                alert("inserted");
            }
        });
    });
</script>

现在一切都很好,但点击按钮后,我收到弹出消息(警报),说数据已插入。相反,我想用灰色显示文本字段——这可能吗?

您可以使用prop()禁用文本输入。这在大多数浏览器中也会使其变灰。试试这个:

success: function(response) {
    $('#name').prop('disabled', true);
}
function disable(){
  $("#name").prop("disabled",true);
}

ajax成功回调后尝试此方法,

    $("#name").attr('readonly', 'true'); // mark it as read only
    $("#name").css('background-color' , '#DEDEDE'); // change the background color