2017-04-27 68 views
1

我在jsfiddle上有以下內容。我需要完成的是當我點擊一個按鈕時,相應的值被插入到空白處。如何分配單擊按鈕的值

https://jsfiddle.net/aminbaig/jefee77L/

這裏是代碼:

HTML:

​​

的Javascript:

function change() { 
 

 
    var myNewTitle = document.getElementById('myTextField').value; 
 
    if (myNewTitle.length == 0) { 
 
    alert('Write Some real Text please.'); 
 
    return; 
 
    } 
 

 
    var title = document.getElementById('title'); 
 
    title.innerHTML = myNewTitle; 
 

 
}

回答

0

你可以這樣做

JS

從功能參數

function change(value) { 
    var title = document.getElementById('title'); 
    title.innerHTML = value; 
} 

HTML

改變按鈕type='submit'type='button'獲取按鈕的值,而且通過該值作爲函數參數

<input type="button" id="myTextField" value="something" onclick="change(this.value)" /> 
<input type="button" id="byBtn1" value="Truck" onclick="change(this.value)" /> 
<input type="button" id="byBtn2" value="Trash" onclick="change(this.value)" /> 

DEMO

0

使用這樣onclick="change(this.value)"通過click功能的。發送輸入的值

function change(val) { 
 
    if (val.length == 0) { 
 
    alert('Write Some real Text please.'); 
 
    return; 
 
    } 
 
    var title = document.getElementById('title'); 
 
    title.innerHTML = val; 
 

 
}
There is <span id="title">___________</span> wrong with this world! 
 
<p> 
 
    Choose the appropriate word 
 
</p> 
 
<input type="submit" id="myTextField" value="something" onclick="change(this.value)" /> 
 
<input type="submit" id="byBtn1" value="Truck" onclick="change(this.value)" /> 
 
<input type="submit" id="byBtn2" value="Trash" onclick="change(this.value)" />

相關問題