[英]JavaScript OnChange select value into input field
I have a form in my code that has some input fields and a select field. 我的代码中有一个表单,其中包含一些输入字段和一个选择字段。 The select field is populated by a table in a db, which also contains a clients name and e-mail.
选择字段由数据库中的一个表填充,该表中还包含一个客户名称和电子邮件。
When the select field is changed, it has to put the clients name and e-mail in the correct input fields. 选择字段更改后,必须将客户名称和电子邮件放在正确的输入字段中。 I have tried some ways, but haven't found the right one yet.
我已经尝试了一些方法,但是还没有找到正确的方法。
Here's my code: 这是我的代码:
<table>
<tr><td>Company</td><td>
<select id="namelist" name="namelist" onchange="updateinput()">
<?php
$result = mysqli_query($con, "SELECT * FROM clients ORDER BY ID");
while($row1 = mysqli_fetch_array($result))
{
$value = $row1['Value'];
$name = $row1['Name'];
echo"<option value='$value'>$name<br>"; } ?>
</select></td></tr>
<tr><td>Name of contact</td><td>
<input required id="namecontact" name="namecontact" type="text" placeholder="Client name"></td></tr>
<tr><td>E-Mail contactpersoon</td><td>
<input required id="email" name="email" type="text" placeholder="E-Mail"></td></tr>
</table>
Much thanks in advance 提前非常感谢
Keep email and name of the client in data-*
attributes so that you can retrieve those values on onchange
event. 将电子邮件和客户端名称保留在
data-*
属性中,以便您可以在onchange
事件中检索这些值。
getAttribute()
returns the value of a specified attribute on the element.getAttribute()
返回元素上指定属性的值。 If the given attribute does not exist, the value returned will either be null or "" (the empty string)如果给定的属性不存在,则返回的值将为null或“”(空字符串)
Try this: 尝试这个:
function updateinput(e) { var selectedOption = e.options[e.selectedIndex]; document.getElementById('namecontact').value = selectedOption.getAttribute('data-name'); document.getElementById('email').value = selectedOption.getAttribute('data-email'); }
<table> <tr> <td>Company</td> <td> <select id="namelist" name="namelist" onchange="updateinput(this)"> <option>Select data</option> <option data-email='abc1@abc.com' data-name='abc1 xyz' value='test'>test1</option> <option data-email='abc2@abc.com' data-name='abc2 xyz' value='test'>test2</option> </select> </td> </tr> <tr> <td>Name of contact</td> <td> <input required id="namecontact" name="namecontact" type="text" placeholder="Client name"> </td> </tr> <tr> <td>E-Mail contactpersoon</td> <td> <input required id="email" name="email" type="text" placeholder="E-Mail"> </td> </tr> </table>
First, you can store the email value using a data-email
attribute in the option
element, assuming the name
is the text in the option then 首先,您可以使用
option
元素中的data-email
属性存储电子邮件值,假设name
是选项中的文本,然后
<table>
<tr><td>Company</td><td>
<select id="namelist" name="namelist" onchange="updateinput()">
<?php
$result = mysqli_query($con, "SELECT * FROM clients ORDER BY ID");
while($row1 = mysqli_fetch_array($result))
{
$value = $row1['Value'];
$name = $row1['Name'];
$email = $row1['Email'];
echo"<option data-email='' value='$value'>$name<br>"; } ?>
</select></td></tr>
<tr><td>Name of contact</td><td>
<input required id="namecontact" name="namecontact" type="text" placeholder="Client name"></td></tr>
<tr><td>E-Mail contactpersoon</td><td>
<input required id="email" name="email" type="text" placeholder="E-Mail"></td></tr>
</table>
then you can have a change event handler for the select 那么您可以为选择添加一个更改事件处理程序
var namelist = document.getElementById('namelist');
namelist.addEventListener('change', function() {
var option = namelist.options[namelist.selectedIndex];
document.getElementById('namecontact').value = option.value;
document.getElementById('email').value = option.dataset.email;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.