我在jsfiddle上有以下内容。我需要完成的是当我点击一个按钮时,相应的值被插入空白处。
https://jsfiddle.net/aminbaig/jefee77L/
以下是代码:
HTML:
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()" />
<input type="submit" id="byBtn1" value="Truck" onclick="change()" />
<input type="submit" id="byBtn2" value="Trash" onclick="change()" />
使用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 :(得分: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;
}
&#13;
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)" />
&#13;
答案 1 :(得分: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)" />