javascript 在下拉列表中选择值动态更改文本框中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24133158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:14:59  来源:igfitidea点击:

select value in dropdown dynamically change value in textbox

javascriptphpajax

提问by swaroop raj

I am working on this form where it has drop down and text box. When value selected in one drop-down should change value in text-box dynamically.

我正在处理这个有下拉菜单和文本框的表单。当在一个下拉列表中选择的值应动态更改文本框中的值时。

for eg if I select a value "3" in select box then

例如,如果我在选择框中选择一个值“3”然后

select ename from emp where eid ='3';

and result should be in text box

结果应该在文本框中

Similarly form has multiple drop-down list with respective text-box.

类似的表单有多个下拉列表和相应的文本框。

Please suggest. Thanks in advance.

请建议。提前致谢。

回答by Jitendra Yadav

Try this one:

试试这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#empID').change(function(){
    $.get('getEmpInfo.php',{empId:$(this).val()},function(data){
    $('#emp_id').val(data);
 });
});
});
</script>
</head>

<body>
<select name="empID" id="empID">
    <option value="">Select Emp</option>
    <option value='1'>Emp1</option>
    <option value='2'>Emp2</option>
    <option value='3'>Emp3</option>
    <option value='4'>Emp4</option>
</select>
<input type="text" name="emp_id" id="emp_id" />
</body>
</html>

AT getEmpInfo.php PAge

在 getEmpInfo.php 页面

<?php
if(isset($_REQUEST['empId'])){
  // connection should be on this page  
    $sql = mysql_query("select ename from emp where eid =".$_REQUEST['empId']);
    $res = mysql_fetch_assoc($sql);
    echo $res['ename'];die;
}
?>

回答by AkshayP

Try this,

试试这个,

Live Demo

现场演示

$(document).ready(function() {

$(document).ready(function() {

$("#name option").filter(function() {
    return $(this).val() == $("#firstname").val();
}).attr('selected', true);

$("#name").live("change", function() {

    $("#firstname").val($(this).find("option:selected").attr("value"));
});

});?

});?

My Be Duplicate : Duplicate

我的重复:重复

回答by Binh LE

For this solutions you can try:

对于此解决方案,您可以尝试:

//set textbox value change dynamic after dropbox had changed.

//设置文本框值在 dropbox 更改后动态更改。

$('#' + yourDropBoxId).change(function ()
{
var dropDownValue = $('#'+ yourId).val();
// set new value for textbox
$('#' + yourTextBoxId).val(dropDownValue); 
});