简体   繁体   English

如何禁用 <input type=“submit” … > 单击按钮时

[英]how to disable <input type=“submit” … > when button is clicked

i'm a newbie with php and html. 我是php和html的新手。 my code is creating button that depend on number of something and this is example of my code. 我的代码正在创建依赖于某些东西的按钮,这是我的代码的示例。

i have 2 php files, page1.php and page2.php 我有2个php文件,page1.php和page2.php

here is page1.php 这是page1.php

<form target="_blank" method="post" action="page2.php">
    <?
    echo '<input type="submit" value="btn" name="btn"><br>';
    ?>
</form>

and this is page2.php 这是page2.php

<?
if(isset($_POST['btn']))
 {
    echo "clicked";
 }
 else
 {
    echo "no event";
 }
?>

what i want is when user click button. 我想要的是当用户单击按钮时。 the button have to disable 该按钮必须禁用

and result should be "clicked" form echo in page2.php 并且结果应在page2.php中以“单击”形式回显

the problem is i dont know how to disable that button and get result form page2 问题是我不知道如何禁用该按钮并从第2页获取结果

thanks for help 感谢帮助

You can disable when form submit :- 您可以在表单提交时禁用:

$('form#id').submit(function(){
    $(this).find(':input[type=submit]').prop('disabled', true);
});

You can do that using jQuery. 您可以使用jQuery来实现。 You need to include the jQuery script. 您需要包括jQuery脚本。 And write your own code for manipulating the events 并编写自己的代码来处理事件

<form target="_blank" method="post" action="page2.php">
    <?
    echo '<input type="submit" value="btn" name="btn" id="submitBtn"><br>';
    ?>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

$(document).ready(function() {

$("#submitBtn").click(function(){
    $("input[type=submit]").attr('disabled','disabled'); // disable the button

   // $("input[type=submit]").removeAttr('disabled') You can enable the button from this code
});

});

 $('form').submit(function(){ $(this).find(':input[type=submit]').attr('disabled','disabled'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form target="_blank" method="post" action="page2.php"> <input type="submit" value="btn" name="btn"> </form> 

It seems that the problem with all of the other answers so far is that when the submit button is disabled, it causes the name - value data pair associated with it to not be posted, since the button gets disabled before the data is posted. 到目前为止,似乎所有其他答案的问题在于,禁用了提交按钮后,由于与其相关联的name - value数据对不会被发布,因为该按钮在发布数据之前已被禁用。 However, submit buttons are usually not used to pass data anyway, so one solution to this problem is to simply associate the data with a separate, hidden input element. 但是,无论如何通常都不会使用submit按钮来传递数据,因此解决此问题的一种方法是简单地将数据与单独的隐藏input元素关联。

HTML: HTML:

<form id="form" target="_blank" method="post" action="page2.php">
      <input type="hidden" name="btn" value="btn"></input>
      <input id="submit-button" type="submit" value="btn">
      <br />
</form>

Notice that I added id s to the form and submit button for selection in JavaScript. 注意,我在表单和提交按钮中添加了id ,以便在JavaScript中进行选择。

JavaScript: JavaScript的:

// Get the form and the submit button.
var form = document.getElementById('form');
var submitButton = document.getElementById('submit-button');

// Disables the submit button.
function disableSubmitButton() {
  submitButton.setAttribute('disabled', '')
}

// Disable the submit button when the form is submitted, with fallback for IE 5-8.
if (form.addEventListener) {
  form.addEventListener('submit', disableSubmitButton, false);
} else {
  form.attachEvent('onsubmit', disableSubmitButton);
}

If you had two different submit buttons, you could also pass a different value to the action page depending on which button was pressed by setting the value of the hidden input element based on which one was pressed: 如果您有两个不同的提交按钮,则还可以通过基于按下哪个按钮来设置隐藏input元素的值,从而根据按下哪个按钮将不同的值传递给操作页面:

HTML: HTML:

<form id="form" target="_blank" method="post" action="page2.php">
  <input id="hidden-input" type="hidden" name="button-pressed" value=""></input>
  <input id="submit-button-1" type="submit" value="Submit Button 1">
  <input id="submit-button-2" type="submit" value="Submit Button 2">
  <br />
</form>

JavaScript: JavaScript的:

// Get the form and the hidden input.
var form = document.getElementById('form');
var hiddenInput = document.getElementById('hidden-input');

// Set the value of the hidden input depending on which button was pressed.
function setHiddenInputValue(event) {
  // Get the id of the button that was pressed.
  pressedButtonId = event.target.id;

  // Set the value of the hidden input element to be the same as the id of the pressed button.
  hiddenInput.setAttribute('value', pressedButtonId);

// Disables the submit button that was pressed.
function disableSubmitButton(event) {
  event.target.setAttribute('disabled', '')
}

// Disable the submit button when the form is submitted, with fallback for IE 5-8.
if (form.addEventListener) {
  form.addEventListener('submit', function(event) {
    // Event object fallback for IE 5-8.
    if (!event) {
      event = window.event;
    }

    setHiddenInputValue(event);
    disableSubmitButton(event);
  }, false);
} else {
  form.attachEvent('onsubmit', function(event) {
    // Event object fallback for IE 5-8.
    if (!event) {
      event = window.event;
    }

    setHiddenInputValue(event);
    disableSubmitButton(event);
  });
}

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

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