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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!