dongpeng0127 2019-07-29 08:31
浏览 69
已采纳

选择选项后显示说明

i'm building kinda of store website and i want to display description of an item after the foreach loop

example:

<select>
$cati = getAllFrom("*", "items", "where Cat_ID = {$category} AND visible = 1", "Item_ID");
 foreach($cati as $drop){

echo '<option  id="' . $drop['item_ID'] . '">' . $drop['Name'] . '</option>';

}
<select>


<div>
item description : 
<?php echo "here i want to display description of the selected item"; ?>
</div?

now under that select button i have an description box that i want to display the selected item description from db column , how i can solve that ?

  • 写回答

2条回答 默认 最新

  • doudao2954 2019-07-29 08:41
    关注

    There are two ways to do this.. Either call a ajax and get the description when the dropdown list will change. Or just add the description in the attribute while looping and then show that in the textbox when the change event fire..

    <select id = "cats">
    <?php 
    $cati = getAllFrom("*", "items", "where Cat_ID = {$category} AND visible = 1", "Item_ID");
    foreach($cati as $drop){
        echo '<option  id="' . $drop['item_ID'] . '" data-description = "'.$drop['Description'].'">' . $drop['Name'] . '</option>';
    } ?>
    <select>
    
    
    <div>
        item description : 
        <p id = "description"></p>
    </div>
    

    First Way:

    <script>
        $(document).ready(function (){
            $('#cats').on('change', function (){
                $('#description').html($(this).find(':selected').attr('data-description'));
            });
        });
    </script>
    

    Second Way:

    <script>
        $(document).ready(function (){
            $('#cats').on('change', function (){
                var itemID = $(this).val();
                $.ajax({
                    method: 'GET',
                    data: {'itemID': itemID},
                    url: 'getDescription.php'
                    success:function (data){
                        $('#description').html(data);
                    }
                })
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题