将HTML的值传递给jQuery文件

时间:2012-08-05 04:27:43

标签: jquery html youtube

我有一个这样的脚本,它运行良好!

<div id="player"></div>

jQuery文件:

$(document).ready(function(){
    //You can alternatively pass an object:

    $('#player').youTubeEmbed({
        video       : 'http://www.youtube.com/watch?v=uyeJXKfAcpc',
        width       : 640,      // Height is calculated automatically
        progressBar : true      // Hide the progress bar
    });
});

但现在我想在输入值中设置youtube id,如下所示:

<div id="player">
    <input type="hidden" name="youtube-id" value="uyeJXKfAcpc" />
</div>

并将其传递给jquery,就像这样:

$(document).ready(function(){
    //You can alternatively pass an object:
    $('#player').youTubeEmbed({
        var yid = $(this).children("input[name='youtube-id']"),
        video       : 'http://www.youtube.com/watch?v=' . yid,
        width       : 640,      // Height is calculated automatically
        progressBar : true      // Hide the progress bar
    });
});

2 个答案:

答案 0 :(得分:3)

您在对象内声明变量,将其移至youTubeEmbed插件上方,也使用.val()方法访问输入值。

var yid = $("input[name='youtube-id']").val();
$('#player').youTubeEmbed({
    video           : 'http://www.youtube.com/watch?v=' + yid,
    width           : 640,      // Height is calculated automatically
    progressBar : true      // Hide the progress bar
});

答案 1 :(得分:0)

根本不需要使用输入。做 -

<div id="player" data-youtube="uyeJXKfAcpc"></div>

你的jQuery -

$('#player').youTubeEmbed({
    video: 'http://www.youtube.com/watch?v=' + $(this).attr('data-youtube'),
    width: 640,
    progressBar: true
});