2015-05-28 24 views
-1

我想輸入視頻名稱到文本框,然後按ENTER按鈕,然後瀏覽器播放我存儲在我的磁盤中的視頻。 但我不知道如何獲得HTML視頻src的文本框值。 此鏈接「love.mpe」已修復。我如何使用輸入文本框的值來替換它? enter image description here 請幫幫我。謝謝。 這裏是我的代碼如何將文本框的值傳遞給HTML和JavaScript中的src鏈接?

<script type="text/javascript"> 
 
\t function nhap_so(value) 
 
\t { 
 
\t \t document.getElementById("txtText").value += value; 
 
\t } 
 
\t function clearText() 
 
\t { 
 
    \t document.getElementById("txtText").value = ""; 
 
\t } 
 
\t function playVideo() 
 
\t { 
 
    \t myVideo.play(); 
 
\t } 
 

 
\t function stopVideo() 
 
\t { 
 
    \t myVideo.pause(); 
 
\t } 
 
\t /*function getValue() 
 
\t { 
 
\t \t var myString = document.getElementById("txtText").value; 
 
\t \t return myString; 
 
\t \t 
 
\t }*/ 
 
</script> 
 
<script type="text/javascript" src="demo_getvalue.js"></script>
<body> 
 
<input name="txtText" type="text" id="txtText" size="20" maxlength="10" disabled="disabled"/> 
 
<br /> 
 
<br /> 
 
<br /> 
 
<input type="button" value="1" width="20" height="20" onclick="nhap_so(1);" id="so_1" /> 
 
<input type="button" value="2" width="20" height="20" onclick="nhap_so(2);" id="so_2" /> 
 
<input type="button" value="3" width="20" height="20" onclick="nhap_so(3);" id="so_3" /> 
 
<br /> 
 
<input type="button" value="4" width="20" height="20" onclick="nhap_so(4);" id="so_4" /> 
 
<input type="button" value="5" width="20" height="20" onclick="nhap_so(5);" id="so_5" /> 
 
<input type="button" value="6" width="20" height="20" onclick="nhap_so(6);" id="so_6" /> 
 
<br /> 
 
<input type="button" value="7" width="20" height="20" onclick="nhap_so(7);" id="so_7" /> 
 
<input type="button" value="8" width="20" height="20" onclick="nhap_so(8);" id="so_8" /> 
 
<input type="button" value="9" width="20" height="20" onclick="nhap_so(9);" id="so_9" /> 
 
<br /> 
 
<input type="button" value="0" width="20" height="20" onclick="nhap_so(0);" id="so_0" /> 
 
<input type="button" value="ENTER" width="20" height="20" onclick="getValue();"/> 
 
<input type="button" value="CLEAR" width="20" height="20" onclick="clearText();"/> 
 
<br /> 
 
<br /> 
 
<video id="myVideo" width="640" height="360"> 
 
    <source src="love.mp4" type="video/mp4"> 
 
</video> 
 
<br /> 
 
<br /> 
 
<input type="button" value="PLAY" width="20" height="20" onclick="playVideo();" /> 
 
<input type="button" value="PAUSE" width="20" height="20" onclick="stopVideo();" />

回答

0

更改getValue()功能changeValue()及其代碼改成這樣:

changeValue() { 
newVal = document.getElementById('txtText').value; 
document.getElementById('myVideo').firstChild.src = newVal + ".mp4"; 
} 

確保註釋掉它,所以它的工作原理。

+0

newVal值沒有「.mp4」?它如何工作? – nistelrooy41001662

+0

其簡單。看我編輯了代碼。感謝您指出。 – Mustaghees

+0

感謝您的幫助,但我認爲document.getElementById(「myVideo」)。innerHTML ='';它會工作 – nistelrooy41001662

0

詞根記憶你的HTML這樣

<video id='videoPlayer' width="320" height="240" controls="controls"> 
    <source id='mp4Source' src="movie.mp4" type="video/mp4" /> 
    <source id='oggSource' src="movie.ogg" type="video/ogg" /> 
</video> 

2.註冊獲取文本框的值

var VideoLink = document.getElementById("myText").value; 

3:現在設置來源

var player = document.getElementById('videoPlayer'); 

     var mp4Vid = document.getElementById('mp4Source'); 

     player.pause(); 

     // Now simply set the 'src' attribute of the mp4Vid variable!!!! 
     // (...using the jQuery library in this case) 

     $(mp4Vid).attr('src', VideoLink); 

     player.load(); 
     player.play(); 
相關問題