使用 javascript 设置按钮的值 [英] Setting a button's value using javascript

查看:15
本文介绍了使用 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 = ...

因为你使用的是

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆