簡體   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