Changing a checkbox's state programmatically in dashcode

前端 未结 8 1619
长发绾君心
长发绾君心 2021-02-15 01:28

Okay, so I\'m trying to change a checkbox\'s state programmatically in dashcode. I\'ve tried:

var checkbox = document.getElementById(\"checkbox\");

// I have tr         


        
相关标签:
8条回答
  • 2021-02-15 01:59

    Dashboard Widgets just run on WebKit technologies, so code valid for Safari should also be valid in Dashcode. Either of the following should work:

    checkbox.checked = true;
    checkbox.setAttribute("checked", "true");
    

    The fact that they are not working indicates there is a problem elsewhere in your code. I would check the line

    var checkbox = document.getElementById("checkbox");     
    

    Correctly assigns an element to the checkbox variable. Also, check the id of your "checkbox" element is valid and correct (not a duplicate, doesn't have a typo, etc).

    0 讨论(0)
  • 2021-02-15 02:08
    var checkbox = document.getElementById("checkbox"); 
    

    there is a problem with this line, it should be

    var checkbox = document.getElementById('#checkbox");
    
    0 讨论(0)
  • 2021-02-15 02:10

    I don't know which browser you used, but when I tested on FF 3.6, it works. just put like this:

    checkbox.checked = false;
    

    while:

    checkbox = document.getElementById('blablabla');
    

    or write like that

    document.getElementById('idhere').checked = false;
    
    0 讨论(0)
  • 2021-02-15 02:11
    checkbox.setAttribute("checked", "checked"); // set
    checkBox.removeAttribute("checked"); // remove
    
    0 讨论(0)
  • 2021-02-15 02:18

    This question has been around a while. Regardless, the following works for us:

    checkbox.childNodes[1].checked = true; checkBox.childNodes[1].checked = false;

    As pointed out in a previous answer, the way Dashcode creates these controls you need to get past the div wrapper, which has the actual ID (checkbox in this example) and set the property for the input, which is child node 1.

    Looking for the actual 'id' of the input would be problematic as you have no control over what id's are assigned to the node. For example if you have two checkboxes then the first one would have 'input' as the id for child node 1 and the second one 'input1', unless, of source you have used 'input' or 'input1' as an id somewhere in your design already!

    There might be another method but I have not found it yet.

    0 讨论(0)
  • 2021-02-15 02:19

    Maybe:

    checkbox.checked = "checked";
    

    Then:

    checkbox.checked = "unchecked";
    
    0 讨论(0)
提交回复
热议问题