Monitoring DOM Changes in JQuery

前端 未结 4 1036
予麋鹿
予麋鹿 2021-01-14 21:14

Is there a way to detect when the disabled attribute of an input changes in JQuery. I want to toggle the style based on the value.

I can copy/paste the same enable/

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 22:02

    Although there are more efficient ways of tackling this problem, one way of handling this is by running a function every xx seconds that will set the required CSS classes on disabled elements:

    window.setInterval(1000,function() {
        $("input:disabled").addClass("disabled");
        $("input:enabled").removeClass("disabled");
    });
    

    This will check all input elements every second. But again, this is a VERY bad solution. You would be better off restructuring your code.

    Without changing too much of your code and HTML, I would do something like this (didn't test it though):

    $("input[name^=Group]").change(function () {
            var disabled = ($(this).val() == "No") ? "disabled" : "";
            var groupName = $(this).attr("name");
    
            $("#" + groupName + "Fields input")
                 .attr("disabled", disabled)
                 .addClass("disabled");
    
            //remove disabled style to all enabled controls
            $("input:enabled)").removeClass("disabled");
        });
    

提交回复
热议问题