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

查看:148
本文介绍了设置使用JavaScript按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,我会看到答案后,觉得自己很蠢,但我一直运行到网络,这我不知道仍然适用周围prehistoric code。我的问题是我如何使用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!

帮助多少AP preciated。

Help much appreciated.

推荐答案

使用的innerHTML 而不是

form.elements["submit-button"].innerHTML = ...

由于您使用的是&LT;按钮&GT; 而不是&LT;输入类型=按钮&GT; ,你需要设置的innerHTML &LT;按钮方式&gt; ■不要根据他们的价值属性的文本

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屋!

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