Programmatically check a checkbox using CoffeeScript

眉间皱痕 提交于 2019-12-12 18:14:32

问题


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


来源:https://stackoverflow.com/questions/11981577/programmatically-check-a-checkbox-using-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!