根据下拉列表值的更改从数据库自动填充文本框

时间:2022-02-19 02:34:16

My jsp page has a select box with all userIds. And I have two text boxes for username and Data of birth. I want to autofill the textboxes when I select a userid from dropdown list.

我的jsp页面有一个包含所有userIds的选择框。我有两个用于用户名和出生数据的文本框。当我从下拉列表中选择用户ID时,我想自动填充文本框。

I have a separate Servlet class and Database class for querying.

我有一个单独的Servlet类和Database类用于查询。

How to automatically the textboxes when I select userid from dropdown ? And i want this textboxes to be not editable. Can someone provide me example code.

从下拉列表中选择userid时如何自动显示文本框?我希望这个文本框不可编辑。有人可以提供我的示例代码。

1 个解决方案

#1


0  

This JS Fiddle showing how to implement it:

这个JS小提琴展示了如何实现它:

var $select = document.getElementById('slct'),
    $un = document.getElementById('un'),
    $dob = document.getElementById('dob'),
    val, arr, username, dob;

// usually this array is obtained from a server response 
arr = [
    	usr1 = ['user1', 'dob1'],
    	usr2 = ['user2', 'dob2'],
    	usr3 = ['user3', 'dob3'],
    	usr4 = ['user4', 'dob4']
    ];

$select.addEventListener('change', function(){
	val = this.value - 1; // because arrays start at 0.
	username = arr[val][0];
    dob = arr[val][1];
    $un.value = username;
    $dob.value = dob;
});
<select id="slct">
    <option></option>
    <option value="1">id 1</option>
    <option value="2">id 2</option>
    <option value="3">id 3</option>
    <option value="4">id 4</option>
</select>
<input type="text" id="un" disabled>
<input type="text" id="dob" disabled>

#1


0  

This JS Fiddle showing how to implement it:

这个JS小提琴展示了如何实现它:

var $select = document.getElementById('slct'),
    $un = document.getElementById('un'),
    $dob = document.getElementById('dob'),
    val, arr, username, dob;

// usually this array is obtained from a server response 
arr = [
    	usr1 = ['user1', 'dob1'],
    	usr2 = ['user2', 'dob2'],
    	usr3 = ['user3', 'dob3'],
    	usr4 = ['user4', 'dob4']
    ];

$select.addEventListener('change', function(){
	val = this.value - 1; // because arrays start at 0.
	username = arr[val][0];
    dob = arr[val][1];
    $un.value = username;
    $dob.value = dob;
});
<select id="slct">
    <option></option>
    <option value="1">id 1</option>
    <option value="2">id 2</option>
    <option value="3">id 3</option>
    <option value="4">id 4</option>
</select>
<input type="text" id="un" disabled>
<input type="text" id="dob" disabled>