使用 javascript 设置按钮的值 [英] Setting a button's value using javascript
问题描述
我确定看到答案后我会觉得自己很愚蠢,但我一直在网络上遇到史前代码,我不确定这些代码是否仍然适用.我的问题是如何使用 javascript 更改按钮(表单内)的值?"这是我现在所拥有的:
I'm sure I'm going to feel stupid after seeing the answer, but I keep running into prehistoric code around the web, which I'm not sure still applies. My question is "How do I change the value of a button (inside a form) using javascript?" Here's what I have right now:
function myFunc(form) {
form.elements["submit-button"].value = "New<br>Text";
"other stuff that actually works."
return false;
}
紧随其后
<form onSubmit="return myFunc(this);">
<button name="submit-button">Original<br>Text</button>
</form>
其他真正有效的东西".实际工作,所以我确定函数被调用并且按钮被找到.我似乎无法将新建
文本"粘贴到按钮中!
The "other stuff that actually works." actually works, so I'm sure the function is getting called and the button is getting found. I just can't seem to stick "New
Text" into the button!
非常感谢帮助.
推荐答案
使用 innerHTML
而不是 value
.
form.elements["submit-button"].innerHTML = ...
因为你使用的是 而不是
,你需要设置
innerHTML
代码>. 的文本不基于 value 属性.
Because you are using a <button>
instead of <input type="button">
, you need to set the innerHTML
. <button>
s do not base their text on the value attribute.
<button> innerHTML </button>
<input type="button" value="value" />
这篇关于使用 javascript 设置按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!