简体   繁体   English

单击后如何禁用 html 按钮而不是提交按钮

[英]how to disable a html button after it has been clicked not a submit button

hi i have a html button and when you press it, it should give you the distances of shops from where you are so when you press it they appear now the problem is because it is in early stages the information is printed everytime the button is pressed i would just like to now how to disable it when it has been pressed once and then reactivate it when another button has been pressed this is not a submit button my code for the button is :嗨,我有一个 html 按钮,当你按下它时,它应该给你商店与你所在位置的距离,所以当你按下它时,它们现在出现问题是因为它处于早期阶段,每次按下按钮时都会打印信息我现在只想知道如何在按下一次时禁用它,然后在按下另一个按钮时重新激活它这不是提交按钮我的按钮代码是:

   <button onclick="getLocation()">Search</button>

any help would be much appreciated thanks in advance任何帮助将不胜感激提前感谢

I think its very clear for you..我想这对你来说很清楚..

Call your getlocation() method inside the click event..在点击事件中调用您的 getlocation() 方法..

Working fiddle工作小提琴

Code代码

$( "#bind" ).click(function() {
   $(this).attr("disabled", "disabled");
   $("#unbind").removeAttr("disabled");
});
$( "#unbind" ).click(function() {
     $(this).attr("disabled", "disabled");
     $("#bind").removeAttr("disabled");
});

Output输出

结果

Do like that:这样做:

    <script type="text/javascript">
    var clicked = false;
    function disableMe() {
        if (document.getElementById) {
            if (!clicked) {
                document.getElementById("myButton").value = "thank you";
                clicked = true;
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
</script>
<button type="button" id="myButton" onClick="return disableMe()" value="OK">

Or, you can do this: onclick="this.disabled=true"或者,您可以这样做: onclick="this.disabled=true"

There are a number of ways to do this.有多种方法可以做到这一点。 Usually, you could use JavaScript to edit the attributes.通常,您可以使用 JavaScript 来编辑属性。 It's best to read on this and ask if you have problems with your coding.最好阅读此内容并询问您的编码是否有问题。

You should find what you need on this post: How to disable html button using JavaScript?您应该在这篇文章中找到您需要的内容: 如何使用 JavaScript 禁用 html 按钮?

You could try something like this:你可以尝试这样的事情:

$('button').click(function(){
  // Add functionality here, then...
  $(this).attr("disabled", true);   
});

This can be solved in several ways, but the most intuitive way would most likely be to feed the application (which this appears to be) which state it is in. Such as:这可以通过多种方式解决,但最直观的方式很可能是向应用程序(似乎是)提供它所处的状态。例如:

var locationSearched = false //global scope of the application
getLocation() {
  if(locationSearched) {
      //do nothing or inform user of the state + wait for alternative button click
  } else {
      //execute the method and request to retrieve the location
      state = true
  }
}

Alternatively this could be passed as a parameter to the method.或者,这可以作为参数传递给方法。 This does seem abit wonky in my opinion tho.在我看来,这确实有点奇怪。 With the solution suggested you can perform logical behavior in relation to the state, making it abit more scalable.使用建议的解决方案,您可以执行与状态相关的逻辑行为,使其更具可扩展性。

Maybe this will work.也许这会奏效。 Using DOM使用 DOM

function myFunction() {
  document.getElementById("myBtn").disabled = true;
}

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

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