我有一行PHP代码:
<input id="sample" name="btnViewTutorial" type="button" />
单击时,将运行javascript:
var sampleButton = document.getElementById("sample");
sampleButton.onclick = function(){
window.open ("/tutorials/sample.php", "mywindow","status=0,toolbar=0,height=480,width=600,directories=0");
}
我想要做的是,当点击该按钮时,/tutorials/sample.php
应该具有按钮传递的值,例如tutorials/sample.php?video=1
或tutorials/sample.php?video=ff56gskkd2
。
如何在值中传递按钮?以及java脚本如何接收值?
答案 0 :(得分:1)
您需要将查询字符串附加到网址。例如,如果视频的值是data
属性,则可以执行以下操作:
<input id="sample" name="btnViewTutorial" type="button" value="View Tutorial" data-video="1"/>
在Javascript中:
var sampleButton = document.getElementById("sample");
sampleButton.onclick = function(){
console.log(this.dataset.video);
window.open ("/tutorials/sample.php?video="+ this.dataset.video,
"mywindow","status=0,toolbar=0,height=480,width=600,directories=0");
}