javascript 使用javascript将输入标签的值设置为另一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14994539/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:05:53  来源:igfitidea点击:

set value of input tag to another value using javascript

javascripthtmlformsinput

提问by darrenc

I have an HTML input tag and I want to change the value of it when clicking on a button, all before submitting the whole form.

我有一个 HTML 输入标签,我想在单击按钮时更改它的值,所有这些都在提交整个表单之前。

I am trying to use getElementById(idOfInput).value = "value I want to set it to", but that doesn't seem to work.

我正在尝试使用 getElementById(idOfInput).value = "value 我想将其设置为",但这似乎不起作用。

Check out the jsfiddle here: http://jsfiddle.net/dchaddock/83NZu/1/

在此处查看 jsfiddle:http: //jsfiddle.net/dchaddock/83NZu/1/

<input id="resetTesting" value="Testing" />
<button type="button" onclck="resetInput();">Testing</button>

function resetInput() {
    document.getElementById("resetTesting").value = "I'm reset!";
}

Thanks!

谢谢!

回答by Blender

You misspelled onclick:

你拼错了onclick

<button type="button" onclck="resetInput();">Testing</button>
                         ^

Demo: http://jsfiddle.net/83NZu/2/

演示:http: //jsfiddle.net/83NZu/2/

回答by darrenc

Here is jsFiddle Link

这是 jsFiddle 链接

http://jsfiddle.net/83NZu/3/

http://jsfiddle.net/83NZu/3/

<input id="resetTesting" value="Testing" />
<button type="button" id="btnSubmit" >Testing</button>

    $(function(){
        $("#btnSubmit").click(function(){
            $("#resetTesting").val('test'); // Set the Value
            alert($("#resetTesting").val()); // Get the Value
        })
    })

回答by bluetoft

Your function isn't defined when your javascript compiler hits your onclick="resetInput();". Place the script before the html and you should be good to go.

当您的 javascript 编译器点击您的 onclick="resetInput();" 时,您的函数未定义。将脚本放在 html 之前,您应该很高兴。

<script type="text/javascript">
    function resetInput() {

    document.getElementById("resetTesting").value = "I'm reset!";
}
</script>
<input id="resetTesting" value="Testing" />
<button type="button" onClick="resetInput();">Testing</button>

http://jsfiddle.net/83NZu/6/

http://jsfiddle.net/83NZu/6/

also... as everyone else mentioned... its onclick ;)

也......正如其他人提到的......它的onclick ;)

edit

This is incorrect. The problem with the original jsfiddle was both the typo and the setting of onLoad vs no-wrap (head) fwiw

这是不正确的。原始 jsfiddle 的问题是 onLoad vs no-wrap (head) fwiw 的错字和设置

回答by user1573365

The problem is the default option "onload" and the error spell onclck:

问题是默认选项“onload”和错误拼写onclck

The "onLoad" option means:wrap the code so it will run in onLoad window event

“onLoad”选项的意思是:包装代码,使其在 onLoad 窗口事件中运行

HTML code:

HTML代码:

<input id="resetTesting" value="Testing" />
<button id="btn" type="button">Testing</button>

JavaScript code:

JavaScript 代码:

document.getElementById("btn").onclick = function(){
  document.getElementById("resetTesting").value="I'm reset";
}

http://jsfiddle.net/83NZu/8/

http://jsfiddle.net/83NZu/8/

or choose the option"no wrap(head)",it means:do not wrap the JavaScript code, place it in <head>section

或者选择选项“no wrap(head)”,意思是:不包装JavaScript代码,放在<head>section中

http://jsfiddle.net/83NZu/7/

http://jsfiddle.net/83NZu/7/