开发者

Disable a button on a specific drop down value

开发者 https://www.devze.com 2023-03-21 14:32 出处:网络
Trying to work out a small issue with dojo, where if the val开发者_运维百科ue of myCode equals selCode (Value selected from dropdown)

Trying to work out a small issue with dojo, where if the val开发者_运维百科ue of myCode equals selCode (Value selected from dropdown) the button needs to be disabled. Where am I going wrong here?

Example

var myCode = "CC";

dojo.addOnLoad(function() {
    dojo.connect(dojo.byId('#codes'), "onchange", function(evt) {
        var selCode = this.val();
        if (selCode == myCode) {
            dojo.attr('#submit', disabled);
        }
        dojo.stopEvent(evt);
    });
});

<select name="codes" id="codes" class="codes">
    <option selected="selected" value="">Select Below</option>
    <option value="AA">AA</option>
    <option value="BB">BB</option>
    <option value="CC">CC</option>
    <option value="DD">DD</option>
</select>


Two issues: 1. Dojo doesn't use CSS selectors like that. Don't use a #. 2. The attr function takes three params (when doing what you want to do): id/dojoObj, attr name, attr value.

var myCode="CC";    
dojo.addOnLoad( function() {
   dojo.connect(dojo.byId('codes'), "onchange", function(evt) {
      if(this.value == myCode){
         dojo.attr('submit', 'disabled', 'disabled');
      }
      dojo.stopEvent(evt);
   });
});


Well, I don't know anything about Dojo, but is sure looks like you're trying to use jQuery methods .val() and .attr(), unless Dojo has the same methods. (I couldn't find docs for them.)

Abandoning the library code for the native API would work like this:

var myCode="CC"; 

dojo.addOnLoad( function() {
     dojo.connect(dojo.byId('#codes'), "onchange", function(evt) {
         var selCode = evt.target.options[evt.target.selectedIndex].value;
         if(selCode == myCode){
             document.getElementById('submit').disabled = true;
         }
         dojo.stopEvent(evt);
     });
 });

Example: http://jsfiddle.net/BmSjb/21/


EDIT: Based on the comment below, you want it to toggle based on matching selection.

var myCode = "CC";

dojo.addOnLoad(function() {
    dojo.connect(dojo.byId('#codes'), "onchange", function(evt) {
        var selCode = evt.target.options[evt.target.selectedIndex].value;
        document.getElementById('submit').disabled = (selCode == myCode);
        dojo.stopEvent(evt);
    });
});

Example: http://jsfiddle.net/BmSjb/32/

Now the .disabled property will be set to the result of selCode == myCode, so when true it will be disabled. When false, enabled.

0

精彩评论

暂无评论...
验证码 换一张
取 消