javascript 如果使用javascript选中复选框,如何重定向到特定链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3690419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:57:45  来源:igfitidea点击:

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

javascripthtmlcheckbox

提问by OM The Eternity

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

如果使用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.com/dropbox?dropbox=mydomain';
            return false;
        }
        return true;

}
</script>

Please help

请帮忙

回答by Li0liQ

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.com/dropbox?dropbox=mydomain';
        return false;
    }
    return true;

}
</script>

Changes:

变化:

  • onclickinstead of onselect
  • checkboxes' checkedproperty is boolean
  • onclick代替 onselect
  • 复选框的checked属性是布尔值

回答by Robert

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

我不相信onselect复选框是一个有效的事件,但我可能错了。

Regardless, this works.

无论如何,这是有效的。

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

Fiddlehttp://jsfiddle.net/Tm6q6/

小提琴http://jsfiddle.net/Tm6q6/

回答by game changer

use this code

使用此代码

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

回答by sushil bharwani

instead of using onselect use onclick event.

而不是使用 onselect 使用 onclick 事件。

and instead of writing

而不是写作

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

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

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

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