最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

function - javascript onclick remove single checkbox - Stack Overflow

programmeradmin2浏览0评论

Now I'm sure this is super easy, I have a function called "remove_deposit" which I want it to make the checkbox false if it's true. But I can't seem to get it to work..

function remove_deposit() {
    if(document.getElementById('deposit').checked == true) {
        document.getElementById('deposit').checked == false;
    };
};

Am I at least on the right track?? lol

Now I'm sure this is super easy, I have a function called "remove_deposit" which I want it to make the checkbox false if it's true. But I can't seem to get it to work..

function remove_deposit() {
    if(document.getElementById('deposit').checked == true) {
        document.getElementById('deposit').checked == false;
    };
};

Am I at least on the right track?? lol

Share Improve this question asked Jul 3, 2009 at 0:21 SoulieBabySoulieBaby 5,47125 gold badges98 silver badges152 bronze badges 1
  • 1 You only need one "=" on the third line. Is that a typo or did you copy paste? – Joel Commented Jul 3, 2009 at 0:24
Add a ment  | 

3 Answers 3

Reset to default 3
function remove_deposit() {
    if(document.getElementById('deposit').checked == true) {
        document.getElementById('deposit').checked = false;
    };
};

You were doing a parison instead of simply setting the checked attribute to false.

function remove_deposit() {
    document.getElementById('deposit').checked = false
}

That's all you need. The reason why your code wasn't working was because you used two equals signs, which is a parison operator, instead of one equals sign, which is the assignment operator.

Addendum: I removed the if statement since it doesn't really do anything useful afaict. If you did this to optimize the code, then I'd just like to point out that checking the if statement will probably be slower than just setting the checkbox to false. Also, you don't need to end every line with a semi-colon in JavaScript. You can do that if you want to put multiple mands on a single line, but otherwise it's not necessary.

Results are in.

If most of the time people running the javascript to set the checkbox to false do have the checkbox set to true, skipping the if statement is faster. Otherwise, if most of the time the checkbox is set to false, then the if statement would be faster. With a 1:1 ratio, no if statement is preferred.

The checkbox would have to be set to false at least 54% of the time for the if code to be more efficient.

http://img33.imageshack.us/img33/4260/results.png http://img33.imageshack.us/img33/4260/results.png

Side-note: If it's a checkbox, it's probably in a form, so you could also access it with the old-fashioned method document.formName.elementName instead of document.getElementById(). Because the form method doesn't need to traverse the DOM like getElementById does, I think that would be faster.

The results are in.

With 0 IDs preceding it, document.test_form.checkbox (DTFC) is slower than document.getElementById('checkbox') (GEBI). With 100 IDs preceding it, document.test_form.checkbox is still slower than document.getElementById('checkbox').

http://img8.imageshack.us/img8/6683/resultsw.png http://img8.imageshack.us/img8/6683/resultsw.png

I guess that settles it.

PS: These were all tested on Firefox using Firebug. I cannot make any claims about the efficiency of Safari's WebKit, Konqueror's KJS, IE's proprietary engine, or Google Chrome's V8. I'm guessing they should wind up somewhat similar to these, but they could be different.

You have an small error in your code sample on line 3. You've used a double equals ==

It should read:

document.getElementById('deposit').checked = false;

发布评论

评论列表(0)

  1. 暂无评论