如何将文本框值传递给Javascript变量?

时间:2013-05-01 05:05:19

标签: javascript asp.net

我需要将asp.net中的文本框值传递给javascript变量videoID并将其保存到数据库中。

这是我的代码

<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubePlayerAPI() {
player = new YT.Player('player', {
height: '350',
width: '600',
videoId: 'ASO_zypdnsQ',
events: {
'onReady': onPlayerReady
}
});
}
</script>

2 个答案:

答案 0 :(得分:1)

试试这个

如果您使用 asp:TextBox 作为视频ID而非尝试此

<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubePlayerAPI() {
player = new YT.Player('player', {
height: '350',
width: '600',
videoId: document.getElementsById('<% ClientID.VideoId %>').value,
events: {
'onReady': onPlayerReady
}
});
}
</script>

如果你使用简单的 html文本框而不是试试这个

<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubePlayerAPI() {
player = new YT.Player('player', {
height: '350',
width: '600',
videoId: document.getElementsByid("your textbox's ID").value,
events: {
'onReady': onPlayerReady
}
});
}
</script>

答案 1 :(得分:0)

如果是你的aspx页面,请按照以下说明

<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubePlayerAPI() {
player = new YT.Player('player', {
height: '350',
width: '600',
videoId: '<% AspCode.VideoId %>',
events: {
'onReady': onPlayerReady
}
});
}
</script>

如果不是Aspx页面,您可以在js文件中定义javascript变量

var videoId = '<% AspCode.VideoId %>';

而不是从变量中获取视频ID。