Programmatically check a checkbox using CoffeeScri

2019-07-11 02:27发布

问题:

How can I programmatically check a checkbox in Coffeescript ?

I know in Javascript, I can use this :

myElement.checked = true

Can I do something like the following in Coffeescript ?

myElement.checked "true"

I have already tried the above but it isn't working for me.

Any help in this regard will be highly appreciated. Thanks.

回答1:

UPDATE:

myElement.checked = "yes";

myElement.checked = null;// for unchecking

So, for your case,

a.checked = b.checked; //i.e. check a if b is checked

Example here


//CoffeeScript
a = {}
a.b = 10
alert a.b

Compiles to..

//javascript
var a;
a = {};
a.b = 10;
alert(a.b);

However, a.b 10 compiles to a.b(10) in javascript, which is not what you want. :)



回答2:

Your myElement is jQuery object and you want to address DOM element:

// HTML
<input class="toggle" type="checkbox"/>


//coffeescript

$('.toggle')[0].checked = true