繁体   English   中英

在更多输入字段中设置值

[英]Set values in more input fields

如何通过JavaScript或jQuery在更多输入字段中设置值(默认值)? 谢谢。

<form method="" action="">
Name: <input type="text" id="id1" value="<?php $value; ?>" placeholder="Your name"> <br><br>
Surname: <input type="text" id="id1" value="<?php $value; ?>" placeholder="Your surname"> <br><br>
Email: <input type="text" id="id1" value="<?php $value; ?>" placeholder="Your email"> <br><br>
</form>

遍历所有输入,然后使用element.value语法is设置默认值。

 Array.from(document.getElementsByTagName("INPUT")).forEach(input => { input.value = "Default value has been assigned"; }); 
 Input 1: <input type="text"> Input 2: <input type="text"> Input 3: <input type="text"> 

您忘记了echo显值。 下面是更新的代码。

<form method="" action="">
Name: <input type="text" id="id1" value="<?php echo $value; ?>" placeholder="Your name"> <br><br>
Surname: <input type="text" id="id1" value="<?php echo $value; ?>" placeholder="Your surname"> <br><br>
Email: <input type="text" id="id1" value="<?php echo $value; ?>" placeholder="Your email"> <br><br>
</form>

诀窍是提供一个选择器,该选择器选择要更改的所有元素。

因为您想更改文本框的所有值,所以以下选择器可以: $("input['type=text']") 并更改或设置其值:只需: $("input['type=text']").val('value');

查看正在运行的示例: https : //jsfiddle.net/2od89uou/1/

要设置jQuery中输入字段的值,可以使用.val()方法:

$('#id1').val("This is a value");// the selector #id1 selects all elements with id "id1"

如果要将多个输入设置为相同的值,则需要使用另一个选择器,因为id被设计为唯一的:

$('.text-input').val("Some other value"); // this will set value of all elements with class text-input

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM