按钮将值传递给javascript

时间:2013-11-13 00:31:05

标签: javascript php

我有一行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=1tutorials/sample.php?video=ff56gskkd2

如何在值中传递按钮?以及java脚本如何接收值?

1 个答案:

答案 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");  
}

演示:http://jsfiddle.net/A65xg/