我的 javascript 链接遇到一些问题
我有一个名为“domein”的隐藏输入字段
我有以下带有 onclick 脚本的 div
<div id="button" onclick="window.open('http://www.mylink.nl/?domein=' + document.getElementById('domein').value ,'_blank') ">
<strong>button text</strong>
</div>
该链接不会在新选项卡中打开,也不会显示网址中隐藏字段的“domein”值。
你们能帮帮我吗?
我的第二个问题:如何让url只有http://www.mylink.nl当 'domein' 值 = 空时。
最佳答案
这有效:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<input type="hidden" id="test" value="test">
<div id="button" onclick="window.open('http://www.example.com/?domein=' + document.getElementById('test').value,'_blank');">
<strong>button text</strong>
</div>
</body>
</html>
新标签页网址:
http://www.example.com/?domein=test
请检查并确保您已使用 id domein 定义隐藏输入...
********************************更新**************** **************
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function submit()
{
if(!document.getElementById("test").value.match(/\S/))
{
window.open('http://www.example.com/','_blank');
}
else
{
window.open('http://www.example.com/?domein=' + document.getElementById('test').value,'_blank');
}
}
</script>
<input type="hidden" id="test" value="test">
<div id="button" onclick="submit()">
<strong>button text</strong>
</div>
</body>
</html>
关于javascript window.open 并添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348230/