使用 javascript 设置按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8542000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a button's value using javascript
提问by Felipe
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:
我确定看到答案后我会觉得自己很愚蠢,但是我一直在网络上遇到史前代码,我不确定这些代码是否仍然适用。我的问题是“如何使用 javascript 更改按钮(表单内)的值?” 这是我现在所拥有的:
function myFunc(form) {
form.elements["submit-button"].value = "New<br>Text";
"other stuff that actually works."
return false;
}
followed by
其次是
<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!
“其他真正有效的东西”。实际工作,所以我确定函数被调用并且按钮被找到。我似乎无法将“新
文本”粘贴到按钮中!
Help much appreciated.
非常感谢帮助。
回答by Will
Use innerHTML
instead of value
.
使用innerHTML
代替value
。
form.elements["submit-button"].innerHTML = ...
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>
代替<input type="button">
,所以您需要设置innerHTML
. <button>
s 的文本不基于 value 属性。
<button> innerHTML </button>
<input type="button" value="value" />
回答by vol7ron
Your code works, but isn't doing what you probably expect. Your followed by shows the text of the button and it's initial value, however your code will change the value before the form is submitted. That means, the receiving page (most often a php/perl/asp script) will see the changed value passed as a GET or POST parameter.
您的代码有效,但没有按照您的预期运行。您的后跟显示按钮的文本及其初始值,但是您的代码将在提交表单之前更改该值。这意味着,接收页面(通常是 php/perl/asp 脚本)将看到作为 GET 或 POST 参数传递的更改值。
If you want to change the text w/o submitting the form you might want to try this:
如果您想在不提交表单的情况下更改文本,您可能想尝试以下操作:
function changeValue(id, newText) {
var el = document.getElementById(id);
el.value = newText; // change the value passed to the next page
el.innerHTML = newText; // change the displayed text on the screen
return false;
}
<form onsubmit="return false;">
<button id="submit-button"
name="submit-button"
onclick="changeValue(this.id,'Some New Text');" >Original<br>Text</button>
</form>
see JS Fiddle here
在这里看到 JS Fiddle
回答by Blender
This might work:
这可能有效:
form.elements["submit-button"][0].innerHTML = "New<br>Text";
form.elements["submit-button"]
probably returns all of the elements with that name
attribute, which is an array.
form.elements["submit-button"]
可能返回具有该name
属性的所有元素,这是一个数组。
回答by thescientist
there's a much better way to do that. use document.getElementById and give the button an ID.
有一个更好的方法来做到这一点。使用 document.getElementById 并给按钮一个 ID。
function myFunc() {
document.getElementById('my_button').innerHTML = "New<br>Text";
//"other stuff that actually works."
return false;
}
<form onSubmit="return myFunc();">
<button id="my_button" name="submit-button">Original<br>Text</button>
</form>
I'm not sure you can put a tag into the value of a button, but who knows. Can't say I've tried it.
我不确定您是否可以将标签放入按钮的值中,但谁知道呢。不能说我试过了。