最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onchange function when selecting drop down menu will populate a textbox - Stack Overflow

programmeradmin0浏览0评论

this is my table in the database

tbl_job_title

ID | Job_title    | description     | 
1  | Accountant   | Handle Accounts |
2  | Dev. Support | Support Centre  |

when selecting the job title from the table, i'm using a drop down menu and here is my code.

<select id="job_title" >
   <option value=""> </option>
     <?php foreach ($job_title as $row): ?>
       <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
       </option>
     <?php endforeach?>
</select>

and I have a textbox that will populate if i select a job_title

<input type="text" id="catch_value">

How do I populate the textbox when I select example is "Accountant", the textbox value must be "Handle Accounts".

this is my table in the database

tbl_job_title

ID | Job_title    | description     | 
1  | Accountant   | Handle Accounts |
2  | Dev. Support | Support Centre  |

when selecting the job title from the table, i'm using a drop down menu and here is my code.

<select id="job_title" >
   <option value=""> </option>
     <?php foreach ($job_title as $row): ?>
       <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
       </option>
     <?php endforeach?>
</select>

and I have a textbox that will populate if i select a job_title

<input type="text" id="catch_value">

How do I populate the textbox when I select example is "Accountant", the textbox value must be "Handle Accounts".

Share Improve this question edited May 12, 2014 at 20:51 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Feb 19, 2014 at 6:10 VincentVincent 8526 gold badges30 silver badges68 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Here is the short & sweet code I have made just for you, feel free to use it! Not required Ajax...

CODE -

<select id="job_title" >
   <option value="" class="job" role=""> </option>
     <?php foreach ($job_title as $row): ?>
       <option value="<?php echo $row['id']; ?>" class="job<?php echo $row['id']; ?>" role="<?php echo $row['description']; ?>"><?php echo $row['title']; ?>
       </option>
     <?php endforeach?>
</select>

Jquery -

$('#job_title').on('change',function(){
    var m = this.value;
    var n = $('.job'+m).attr('role');
    $('#catch_value').val(n);
    return false;
});

eg - fiddle

<select id="job_title" onchange="javascript:callajaxvalue(this.value);">
   <option value=""> </option>
     <?php foreach ($job_title as $row): ?>
       <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
       </option>
     <?php endforeach?>
</select>

<script>
    function callajaxvalue(id)
    {
          $.ajax({
           type: "POST",
           url: "some.php",
           data: { id: id}
            success: function(data, textStatus ){
                if(data)
                { $("#catch_value").val(data);}
            },
          })
    }
</script>

some.php

$_POST['id'];

$query = "get data from table";

execute query.

echo $row['description'];

For codeigniter

function callajaxvalue(id)
    {
          $.ajax({
           type: "POST",
           url: "my_controller/update/"+id,
           data: { id: id}
            success: function(data, textStatus ){
                if(data)
                { $("#catch_value").val(data);}
            },
          })
    }

my_Controller.php

    public function update()
    {
        $id = $this->input->post('id');
        $data['discription'] = call model function for get data form database

        echo json_encode($data);
    }
 $('#job_title').change(function(){
  $('#catch_value').val('Handle Accounts');
 }); 

see fiddle:fiddle

or you can add if statments

 $('#job_title').change(function(){
    var jt = $('#job_title').val();
if(jt == "Something")
{
  $('#catch_value').val('Handle Accounts');
}
if (jt == "SomethingElse")
{
   $('#catch_value').val('somethingelse');
}
 }); 

This is the right answer :

    <select id="job_title" onchange='populate()'>
   <option value=""> </option>
    <?php foreach ($job_title as $row): ?>
    <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
   </option>
 <?php endforeach?>
  </select>

   <script>
       function populate(){
        var jobttl= document.getElementById('job_title').value();
        if(jobttl){
          document.getElementById('catch_value').value;
        }

        }
   </script>
发布评论

评论列表(0)

  1. 暂无评论