2017-03-18 41 views
1

我有一個iframe網址,我想根據用戶輸入的文本字段進行更改。所以我有:如何根據來自文本字段的用戶輸入更改iframe URL?

<iframe 
    src="http://player.twitch.tv/?channel={CHANNEL}" 
    height="720" 
    width="1280" 
    frameborder="0" 
    scrolling="no" 
    allowfullscreen="true"> 
</iframe> 

我想將{CHANNEL}更改爲用戶輸入到文本字段並通過按鈕提交。例如,如果用戶在文本字段中輸入「TestName」,則新的iframe URL將變爲src =「http://player.twitch.tv/?channel=TestName」

不知道如何執行此操作。

+1

你有任何的代碼?我們可以幫助您解決代碼中的特定問題,但我們不會爲您編寫代碼。你可以通過JavaScript事件來改變iframe的src屬性。 – Canolyb1

回答

1

試試這個。請記住輸入未經驗證/保護。

function changeChannel(){ 
 
    document.getElementById("twitchFrame").src = "http://player.twitch.tv/?channel="+document.getElementById("channel").value; 
 
}
<input type="text" id="channel"></input> 
 
<button type="button" onClick="changeChannel();">Change Channel</button> 
 
<iframe 
 
    id="twitchFrame" 
 
    src="about:blank" 
 
    height="720" 
 
    width="1280" 
 
    frameborder="0" 
 
    scrolling="no" 
 
    allowfullscreen="true"> 
 
</iframe>

相關問題