简体   繁体   English

如何在javascript中设置表单的输入值?

[英]How can I set the input value of a form in javascript?

Suppose that I have defined a form ( my_form ) that has an input named myInput .假设我定义了一个表单 ( my_form ),它有一个名为myInput的输入。 I would like to set the value of myInput to be my_string .我想将myInput的值设置为my_string

$('#my_form textarea[name=myInput]').value = my_string;

I've tried the above line, and this works for most browsers, but not for Opera/Safari.我已经尝试了上面的行,这适用于大多数浏览器,但不适用于 Opera/Safari。 I know it doesn't work for Opera/Safari because the line我知道它不适用于 Opera/Safari,因为这条线

console.log($('#my_form textarea[name=myInput]').value + " was the string");

logs undefined was the string to the screen instead of the actual value of my_string (which is not undefined). logs undefined was the string屏幕上undefined was the string ,而不是 my_string 的实际值(不是 undefined)。

How can I achieve this for these browsers?我怎样才能为这些浏览器实现这一点?

Try this example:试试这个例子:

 $(document).ready(function () { $("#btn1").click(function () { $("#test1").text("Hello world!"); }); $("#btn2").click(function () { $("#test2").html("<b>Hello world!</b>"); }); $("#btn3").click(function () { $("#test3").val("Dolly Duck"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="test1">This is a paragraph.</p> <p id="test2">This is another paragraph.</p> <p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p> <button id="btn1">Set Text</button> <button id="btn2">Set HTML</button> <button id="btn3">Set Value</button>

You have to use .val() instead of .value .您必须使用.val()而不是.value And quotes for a string.和字符串的引号。

So the final code will be:所以最终的代码将是:

$("#my_form textarea[name=myInput]").val("my_string_with_quotes_before_and_after");

or with variable或带有变量

var string="some text";
$("#my_form textarea[name=myInput]").val(string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法设置表单输入javascript的值 - can't set value of form input javascript 如何使用Javascript设置没有ID的表单输入字段的值? - How to set the value of a form input field without ID using Javascript? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何使用 javascript 获得 {{form}} 值? - How can i get {{form}} value with javascript? 如何在不提交表单的情况下获取javascript中输入字段的值? - How can I get the value of an input field in javascript without submitting a form? 如何使用带有输入值的 javascript dom 操作提交表单 - How can I submit form using javascript dom manipulation with input value 如何通过JavaScript设置输入值? - how to set value in input by JavaScript? 如何摆脱JavaScript中的表单输入值缓存​​? - How do I get rid of form input value cache in JavaScript? 当我检查元素时,如何不仅使用视图设置Java值,还使用元素设置永久值? - How can I Set the Input value Using Javascript not just for view but permanent value in element when I inspect element? 如何将输入的值设置为与所选收音机的值相同? - How can I set the value of an input the same as the value of the radio selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM