选择onchange更新其他输入字段

时间:2016-06-13 10:04:46

标签: javascript php jquery

我有一个带有一个选择选项和3个输入文本框的表单。我从数据库中获取select选项的值。我想根据我选择的内容更改3输入字段的数据。我尝试了许多方法,但发誓。

<Select onchange="update(this)">
<?php 
    $query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
    $run = mysqli_query($connect, $query);
    $i = 0;
    while ($chair = mysqli_fetch_assoc($start)) {
    $i++;
    ?>
<option value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
     $name = $chair['name'];
     $phone = $chair['phone'];
     $detail = $chair['detail'];
     }
?>

它为Select菜单完美地获取数据,但只存储名称phone和detail变量的最后一个值。这是javascript代码。

function update( elem ) {
             var name = "<?php echo $name; ?>";
             var phone= "<?php echo $phone; ?>";
             var detail = "<?php echo $detail; ?>";
             document.getElementById("name").innerHTML = name;
             document.getElementById("phone").innerHTML = phone;
             document.getElementById("detail").innerHTML = detail;
}

任何帮助都是适当的。提前谢谢。

2 个答案:

答案 0 :(得分:1)

在名称,电话和详细信息的选项中设置diffirent属性

演示:https://jsfiddle.net/sjhhv4pw/

<select onchange="update(this)">
<?php 
    $query = "SELECT * FROM wheel1 ORDER BY chair_no ASC";
    $run = mysqli_query($connect, $query);
    $i = 0;
    while ($chair = mysqli_fetch_assoc($start)) {
    $i++;
    ?>
<option data-name="<?php echo $chair['name']; ?>" data-phone="<?php echo $chair['phone']; ?>" data-detail="<?php echo $chair['detail']; ?>" value="<?php echo $chair['chair_no'];?>"><?php echo $i ?></option>
<?php
    }
?>
</select>

jQuery的:

function update( elem ) {
     var name = $(elem).find("option:selected").attr("data-name");
     var phone= $(elem).find("option:selected").attr("data-phone");
     var detail = $(elem).find("option:selected").attr("data-detail");
}

答案 1 :(得分:0)

尝试以下代码

<Select onchange="update(this)">
&#34;数据电话=&#34;&#34;数据椅的细节=&#34;&#34;值=&#34;&#34;&GT;

javascript代码,考虑到你正在使用jQuery

function update( elem ) {
         var name = ele.data('chair-name');
         var phone= ele.data('phone');
         var detail = ele.data('chair-detail');

}