Enable/Disable a button using dojo toolkit

时间:2015-10-30 21:37:01

标签: javascript dojo

I have declared a button in HTML and I want to dynamically enable/disable it using javascript and the Dojo toolkit.

I have made the following code, which should be a minimal working example:

<html>
<head>
<script language="JavaScript" type="text/javascript">
require(["dojo/dom", "dojo/on", "dojo/request", "dojo/domReady!"],
        function(dom, on, request){
            on(dom.byId("password"), "keyup", function(evt){
                request.get("/PasswordChecker",
                        {
                            query: {
                                user: document.getElementById("username").value,
                                pass: document.getElementById("password").value
                            }
                        }).then(
                    function(response){
                        if(strength === "STRONG") {
                            dijit.byId("ok").setAttribute('disabled',false);
                        }
                    }
                );
            });
        }
    );
</script>
</head>

<body>
<input id="username" type="text"/>
<input id="password" type="text"/>
<button id="ok" dojoType="dijit.form.Button" type="submit" disabled="disabled">OK</button>
</body>
</html>

In other words I want the user to enter a password. I will then use ajax to check if it is strong enough and if so want to enable the OK-Button.

1 个答案:

答案 0 :(得分:0)

I rewrote the code as follows:

function updateOkButton(strength) {
    if(strength === "STRONG") {
    //  domAttr.set(okButton, 'disabled', false);
        require(['dojo/on', 'dojo/dom', 'dojo/dom-attr'],
        function (on, dom, domAttr) {
            var okButton = dom.byId("ok");
            domAttr.set(okButton, 'disabled', false);
        });
    }
}