我希望打开一个窗口,具体取决于复选框中选中的内容。如果两者都被选中,它应该是fb或twitter或两个窗口都打开。
<script type="text/javascript">
var checky = document.getElementById('twitter');
function doalert(checkboxElem) {
if (checky.checked)
{
window.open("https://twitter.com/intent/tweet?url=http://www.myurl.com& text=" + document.forms["frm1"]["fname"].value + "");
}
else
{
window.open("http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.myurl.com%2Fimages%2Fcrowd_support.jpg");
}
}
</script>
<form name="frm1" action="select_demo.html" onsubmit="doalert(this)">
<label><input type="checkbox" id="twitter" name="twitter" value="false">Twitter</label><label><input type="checkbox" name="facebook" value="">Facebook</label>
<br>
<textarea name="fname" cols="68" rows="3"></textarea>
<br>
<input type="submit" value="Submit" />
</form>
答案 0 :(得分:0)
您需要检查函数内是否选中了复选框,如下所示。在函数外部,将在页面加载时取消选中
<script type="text/javascript">
function doalert(checkboxElem) {
var checkytw = document.getElementById('twitter');
var checkyfb = document.getElementById('facebook');
if (checkytw.checked)
window.open("https://www.twitter.com");
if(checkyfb.checked)
window.open("http://www.facebook.com");
}
</script>
<form name="frm1" action="select_demo.html" onsubmit="doalert(this)">
<label><input type="checkbox" id="twitter" name="twitter" value="false">Twitter</label><label><input type="checkbox" name="facebook" id="facebook" value="">Facebook</label>
<br>
<textarea name="fname" cols="68" rows="3"></textarea>
<br>
<input type="submit" value="Submit" />
</form>