简体   繁体   English

如何在提交后禁用输入字段

[英]How to disable input field after submit

I need help disabling an input field after the submit button is clicked. 单击提交按钮 ,我需要帮助禁用输入字段。 I'm currently using Laravel, so I'm not sure if the jQuery is placed somewhere else other than the view. 我目前正在使用Laravel,所以我不确定jQuery是否放在除了视图之外的其他地方。 I tried putting this jQuery code in index.blade.php, but it didn't work. 我尝试将这个jQuery代码放在index.blade.php,但它没有用。

<script type="text/javascript">
    $(document).onclick(function(){
         $("#pid :input").prop("disabled", true);
    });
</script>

Just listen for a submit click, then set the Boolean disabled attribute of the input: 只需听取提交点击,然后设置输入的布尔disabled属性:

$("#submit").on("click", function() {
    $("#pid").attr("disabled", "disabled");
});

Check my code.I have created an example of a form and input element. 检查我的代码。我创建了一个表单和输入元素的示例。

For more info about serialization check this link 有关序列化的更多信息,请查看此链接

You will send using ajax the input elements values to file.php, then the echo from the file.php will send to ajax call the data and the data will be printed in console. 您将使用ajax将输入元素值发送到file.php,然后来自file.php的echo将发送到ajax调用数据,数据将在控制台中打印。

After that the input field will be disabled. 之后,输入字段将被禁用。

file.php file.php

<?php 
header('Access-Control-Allow-Origin: *');

if(isset($_GET['nm'])){
    $name = $_GET['nm'];

    echo $name;
}

?>

 $("#frm1").submit(function(e){ //make sure you have name value in each input element $.ajax({ method:'get', url:'http://localhost/file.php', data:$(this).serialize() }).done(function( data ) { console.log(data); }).fail(function( jqXHR, textStatus ) { alert( "Request failed: " + textStatus ); }); //after submit disable the input elememt $("#nm").prop("disabled", true); e.preventDefault(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="frm1" action="" method="post"> <input type="text" id="nm" name="nm" /> <input type="submit" value="Submit"/> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM