2010-09-11 33 views
2

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

我这样做,但它不是为我工作..

<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> 

请帮

回答

5

有一些问题,你的源。这里是工作的版本:

<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> 

变化:

  • onclick,而不是onselect
  • 复选框checked属性是布尔
3

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

无论如何,这工作。

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

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

0

而是采用ONSELECT使用onclick事件。

,而不是写

如果(的document.getElementById( 'yousendit')。检查== “选中”)和

写如果(的document.getElementById( 'yousendit')。选中)

2

使用此代码

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