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

html - How to redirect to a particular link if checkbox is checked using javascript? - Stack Overflow

programmeradmin5浏览0评论

How to redirect to a particular link if checkbox is checked using javascript?

I am doing this but its not working for me..

<input type="checkbox" name="yousendit" id="yousendit" value="1" onselect="return yousendit();"/>

<script type=javascript>
function yousendit()
{
        if(document.getElementById('yousendit').checked== "checked")
        {
            window.location='';
            return false;
        }
        return true;

}
</script>

Please help

How to redirect to a particular link if checkbox is checked using javascript?

I am doing this but its not working for me..

<input type="checkbox" name="yousendit" id="yousendit" value="1" onselect="return yousendit();"/>

<script type=javascript>
function yousendit()
{
        if(document.getElementById('yousendit').checked== "checked")
        {
            window.location='https://www.yousendit./dropbox?dropbox=mydomain';
            return false;
        }
        return true;

}
</script>

Please help

Share Improve this question edited May 28, 2012 at 7:42 halkazzar 751 silver badge7 bronze badges asked Sep 11, 2010 at 9:10 OM The EternityOM The Eternity 16.2k44 gold badges125 silver badges187 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

There are some problems with your source. Here is the working version:

<input type="checkbox" name="yousendit" id="yousendit" value="1" onclick="return yousendit();"/>
<script>
function yousendit(){
    if(document.getElementById('yousendit').checked){
        window.location='https://www.yousendit./dropbox?dropbox=mydomain';
        return false;
    }
    return true;

}
</script>

Changes:

  • onclick instead of onselect
  • checkboxes' checked property is boolean

I don't believe onselect is a valid event for a checkbox, but I may be wrong on that.

Regardless, this works.

document.getElementById('yousendit').onclick = function() {
    if (this.checked==true)
        alert('checked'); // Or in your case, window.location = 'whatever.html';
}​​​​​​

Fiddle http://jsfiddle/Tm6q6/

use this code

<input type="checkbox" value="xyz.php"
    name="checket"
    onClick="if (this.checked) { window.location = this.value; }">

instead of using onselect use onclick event.

and instead of writing

if(document.getElementById('yousendit').checked== "checked")

write if(document.getElementById('yousendit').checked)

发布评论

评论列表(0)

  1. 暂无评论