2014-10-31 41 views
0

我有兩個文本輸入元素:的onsubmit改變DOM元素和屬性的值

<input type="text" id="Title"/> 
<input type="text" id="URL"/> 

一個下拉菜單和一個提交按鈕。 select下拉菜單有幾個選項,其值等於頁面中元素的ID。

如果您單擊提交按鈕,它將更改/填充在下拉菜單中選擇ID的div元素中的值。

<img src="https://www.google.com/s2/favicons?domain=$URL" alt="Icon" title="icon"> 
<a href="$URL" title="$TITLE"> $TITLE </a><br /> 

進入與在下拉菜單中選擇的ID的div。

標題輸入值= 「谷歌」
URL輸入值= 「http://www.google.com
選擇選項ID = 「Search_Engines」

而且你會按「提交」

然後,永久寫入元素的ID爲Search_Engines

<img src="https://www.google.com/s2/favicons?domain=http://www.google.com" alt="Icon" title="icon"> 
<a href="http://www.google.com" title="Google"> Google </a><br /> 

有沒有反正這可以實現?

+0

'永久寫'在改變你的HTML? – briansol 2014-10-31 14:04:38

+0

使用Javascript,您可以訪問要更改的屬性。這是基本的Javascript,你應該能夠在網上找到它, 「如何更改圖像源javascript」 「獲取輸入值javascript」 「Onsubmit事件javascript」 – Alex 2014-10-31 14:06:10

回答

0

的Javascript:

<script> 
     function change(){ 
      var Title = document.getElementById('Title').value; 
      var URL = document.getElementById('URL').value; 
      var Element = document.getElementById('Element').value; 
      var div = document.getElementById(Element); 
      div.innerHTML = Title + " " + URL; 
      return false; 
     } 
    </script> 

和HTML表單

<form> 
     <input type="text" name="Title" id="Title"><br /> 
     <input type="text" name="URL" id="URL"><br /> 

     <select name="Element" size="1" id="Element"> 
      <option>1</option> 
      <option>2</option> 
     </select> 
     <input type="submit" name="submit" value="submit" onclick="return change()"> 
    </form> 
    <div id="1">text</div> 
    <div id="2">text</div> 

想有更好的解決方案,但作品。

+0

這實際上並沒有寫入輸入文件。它只是暫時改變文字。 – 2014-10-31 21:06:20