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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable