2012-08-05 104 views
-3

我有這樣的腳本,它運行良好!將HTML的值傳遞給jQuery文件

<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 
    }); 
}); 
+1

你能更具體與您的問題嗎? – SomeKittens 2012-08-05 04:30:56

+0

我只需要存儲隱藏輸入的值,並將其傳遞給上述javascript文件中的varible,並在'http://www.youtube.com/watch?v='作爲變量 – user1576910 2012-08-05 04:38:57

回答

3

Y ou在對象中聲明變量,將其移至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 
}); 
+0

無法工作後回顯此值att all – user1576910 2012-08-05 04:44:08

+0

@ user1576910你是否觸發了JavaScript(例如某種按鈕點擊)? – Sam 2012-08-05 04:45:55

+0

@ user1576910看到更新 – Musa 2012-08-05 04:46:25

0

根本沒有必要使用輸入。做 -

​​

你的jQuery -

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

不能正常工作,請下載完整的腳本併爲我嘗試:http://www.4shared.com/rar/XoaJijx5/var_pass.html – user1576910 2012-08-05 05:37:26

+0

請在線發佈,所以我們不必下載它 – 2012-08-05 05:41:41

+0

解決方案是這裏:http://stackoverflow.com/questions/11814183/i-want-to-store-a-value-in-jquery-var-and-passit – user1576910 2012-08-05 05:42:28