使用jquery禁用启用按钮 [英] disable enable button using jquery

查看:93
本文介绍了使用jquery禁用启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的javascript版本来禁用/启用一个按钮,但我无法使用jQuery工作。

I have a working javascript version to disable/enable a from button but I can not get it working using jQuery.

我的jsfiddle有两个版本。 javascript版本已注释掉。

My jsfiddle has both versions. The javascript version is commented out.

    //NOT WORKING jQuery
   function controls(id) {
   if (id === "button_start") {
    //$('button_start').prop('disabled','disabled');
    // $('button_stop').removeProp('disabled','disabled');

    // testing
    $('#button_start').on('click',function() {
        $(this).prop('disabled', 'disabled');
    });
    $('#button_stop').on('click', function(){
        $(this).removeProp('disabled', 'disabled');
    });

    //console.log(id);

   } else {
    //$('button_stop').prop('disabled','disabled');
    //$('button_start').removeProp('disabled','disabled');

     // testing
    $('#button_stop').click(function() {
        $(this).prop('disabled', 'disabled');
    });
    $('#button_start').click(function(){
        $(this).removeProp('disabled', 'disabled');
    });
      //console.log(id);
        }
    }

jsFiddle: https://jsfiddle.net/tommy6s/w2u8eskv/

jsFiddle: https://jsfiddle.net/tommy6s/w2u8eskv/

谢谢你的支持帮助!

推荐答案

这可能无法解决您的问题,但是您使用的是 removeProp 方法错了。根据jQuery文档,removeProp只接受一个属性

This might not solve your problem, however you are using the removeProp method wrong. According to the jQuery documentation, removeProp takes only one attribute

.removeProp( propertyName )
propertyName
Type: String
The name of the property to remove.

在您的示例中,我会更改看起来像这样的行

In your example, I would change your lines that look like this

$('#button_start').click(function(){
    $(this).removeProp('disabled', 'disabled');
});

到此

$('#button_start').click(function(){
    $(this).removeProp('disabled');
});

https://api.jquery.com/removeProp/

另外,请记住id元素必须以开头#签收。你在你的OP中有这个但不是小提琴。

Also, remember that id elements must start with the # sign. You had that in your OP but not in the fiddle.

这篇关于使用jquery禁用启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆