dongtui8593 2015-09-30 09: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 09: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部