Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript禁用按钮_Javascript_Jquery_Html_Css_Dojo - Fatal编程技术网

Javascript禁用按钮

Javascript禁用按钮,javascript,jquery,html,css,dojo,Javascript,Jquery,Html,Css,Dojo,我想知道是否有人能帮我解决这个问题 我有一个按钮定义为: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> 我面临的问题是,即使此代码禁用了按钮,如果我单击按钮,onClick事件仍然会触发函数callMe() 如何使禁用的按钮不调用onClick函数?使用jQuery,您可以绑定一个函数来检查按钮是否禁用: $('#myButton').click(f

我想知道是否有人能帮我解决这个问题

我有一个按钮定义为:

<input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/>
我面临的问题是,即使此代码禁用了按钮,如果我单击按钮,onClick事件仍然会触发函数
callMe()


如何使禁用的按钮不调用onClick函数?

使用jQuery,您可以绑定一个函数来检查按钮是否禁用:

$('#myButton').click(function(){
    if( !$(this).is('[disabled=disabled]') ){
       ...
    }
});
$('#myButton').click(function() {
  if (!$(this).is(':disabled')) {
    callMe();
  }
});

使用jQuery,您可以绑定一个函数来检查按钮是否被禁用:

$('#myButton').click(function() {
  if (!$(this).is(':disabled')) {
    callMe();
  }
});

在禁用onclick事件处理程序的同一代码中,只需删除onclick事件处理程序。

在禁用onclick事件处理程序的同一代码中,只需删除onclick事件处理程序。

因为您使用的是jQuery,所以使用

$('#myButton').click(function() { callMe(); this.unbind(); });

因为您使用的是jQuery,所以使用

$('#myButton').click(function() { callMe(); this.unbind(); });
您应该使用jQuery来附加单击处理程序,而不是内联添加它们,而不是onClick。

*

只需在单击功能中添加一个复选框

$('#myButton').click(function(){
  var $this;
  $this = $(this);
  if ( $this.is(':disabled') ) return;
  callMe();
});
或者

$('#myButton').click(callMe);

callMe()
{
  var $this;
  $this = $(this);
  if ($this.is(':disabled')) return;
  ...the rest of your code...
}
或者如果您坚持在线使用它:

onclick="if ($(this).is(':disabled')) return; callMe();"

*我符合MVC的定义

您应该使用jQuery附加单击处理程序,而不是内联添加它们*

只需在单击功能中添加一个复选框

$('#myButton').click(function(){
  var $this;
  $this = $(this);
  if ( $this.is(':disabled') ) return;
  callMe();
});
或者

$('#myButton').click(callMe);

callMe()
{
  var $this;
  $this = $(this);
  if ($this.is(':disabled')) return;
  ...the rest of your code...
}
或者如果您坚持在线使用它:

onclick="if ($(this).is(':disabled')) return; callMe();"


*我符合MVC的定义,而不是使用onclick属性,绑定到事件

$('#myButton').click(function(e){
    // try it without this
    if($(this).attr('disabled'))
        return false;
    callMe();
    e.preventDefault();
});

在不检查的情况下尝试,不确定是否有必要。

不要使用onclick属性,而是绑定到事件

$('#myButton').click(function(e){
    // try it without this
    if($(this).attr('disabled'))
        return false;
    callMe();
    e.preventDefault();
});

不用检查就试试,不确定是否有必要。

这是一种方法

$('#myButton').click(function(){
  if(!$(this).hasClass("disabled")){
     //Do stuff here
  }
});
要禁用按钮,只需将禁用的类添加到按钮中

$('#myButton').addClass("disabled");

在disabled类中添加适当的样式,使按钮看起来像disabled,或者您也可以在设置类的同时设置disabled属性。

这是一种方法

$('#myButton').click(function(){
  if(!$(this).hasClass("disabled")){
     //Do stuff here
  }
});
要禁用按钮,只需将禁用的类添加到按钮中

$('#myButton').addClass("disabled");
在disabled类中添加适当的样式,使按钮看起来像disabled,或者您也可以在设置类的同时设置disabled属性。

而不是

$('#myButton').attr('disabled', 'disabled');
。。。使用

$('#myButton')
   .prop('disabled', 'disabled')  // Sets 'disabled' property
   .prop('onclick', null)         // Removes 'onclick' property if found
   .off('click');                 // Removes other events if found
而不是

$('#myButton').attr('disabled', 'disabled');
。。。使用

$('#myButton')
   .prop('disabled', 'disabled')  // Sets 'disabled' property
   .prop('onclick', null)         // Removes 'onclick' property if found
   .off('click');                 // Removes other events if found

检查callMe中的按钮是否被禁用?在旁注中,您也可以传递
true
attr('disabled',true)
,只是为了让它更清楚一点。@Simon,好主意,但这需要在函数中添加此检查,即使应用程序的某些部分不需要此检查functionality@pimvdb,您可以试试您的解决方案。检查callMe中的按钮是否已禁用?在旁注中,您还可以传递
true
attr('disabled',true)
,只是为了让它更清楚一些。@Simon,好主意,但这需要在函数中添加此检查,即使应用程序的某些部分不需要此检查functionality@pimvdb,可以试试你的解决方案。嗨,mblase,实际上我现在正在使用这个的标准Javascript。嗨,mblase,实际上我现在正在使用这个的标准Javascript。嗨,Jonathan,如何删除onClick事件处理程序?嗨,乔纳森,如何删除onClick事件处理程序?嗨,我不坚持使用内联。我更喜欢更好的解决方案,如果你说内联不好,我不会使用它:)@t0mcat,我已经用链接更新了我的答案,这些链接指向了你应该避免内联JS的原因。嗨,Zzzz Bov,我不坚持使用内联。我更喜欢更好的解决方案,如果你说内联不好,我不会使用它:)@t0mcat,我已经更新了我的答案,并链接到了你应该避免内联JS的原因。