【问题标题】:How to redirect to a particular link if checkbox is checked using javascript?如果使用javascript选中复选框,如何重定向到特定链接?
【发布时间】:2016-05-21 12:31:30
【问题描述】:
如果使用 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>
请帮忙
【问题讨论】:
标签:
javascript
html
checkbox
【解决方案1】:
您的来源存在一些问题。这是工作版本:
<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
- checkboxes 的
checked 属性是布尔值
【解决方案2】:
我不相信onselect 是复选框的有效事件,但我可能错了。
不管怎样,这行得通。
document.getElementById('yousendit').onclick = function() {
if (this.checked==true)
alert('checked'); // Or in your case, window.location = 'whatever.html';
}
小提琴 http://jsfiddle.net/Tm6q6/
【解决方案3】:
使用此代码
<input type="checkbox" value="xyz.php"
name="checket"
onClick="if (this.checked) { window.location = this.value; }">
【解决方案4】:
使用 onclick 事件而不是使用 onselect。
而不是写作
if(document.getElementById('yousendit').checked== "checked")
写 if(document.getElementById('yousendit').checked)