dongtui8593 2015-09-30 17:07
浏览 17
已采纳

使用所选选项中的数据库值填充textarea [关闭]

I have a select element which has values of sequences in a database:

<option value="1">one</option>
<option value="2">two</option>

I then have this PHP code which puts the table data into an array

<?php
$return_arr = array();
$sql="SELECT * from tickets_standardresponses ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs)) {
    $return_arr[] = array('sequence' => $result["sequence"], 'response' => $result["response"]);
}
$data = json_encode($return_arr);
?>
<script type="text/javascript">
$(document).ready(function(){
    var data = <?php echo $data; ?>;
    $('#standard_response').on('change',function() {

    });
});
</script>

How can I fill a textarea based on the selected option

For example, if the selected option's value = 1 i want to put the response data from the array where the sequence = 1

I do not want to use GET/POST requests using Ajax

  • 写回答

3条回答 默认 最新

  • dongqin6926 2015-09-30 17:25
    关注
            var data = <?php echo $data; ?>;
    
            $('#response').on('change', function() {
    
                var sequence = $(this).val();
                //Check each object in the data array
                $.each(data, function( index, obj ) {
                    if (obj.sequence === sequence) {
                        //The "result" textarea
                        $('#result').text(obj.response);
                    }
                });
            });
    

    You have to loop through the data array and check each objects sequence and see if it matches the value of the selected option.

    Note: Make sure that the sequence parameter is a string or convert the option value to a integer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line