dourui9570 2018-08-13 22:43
浏览 56
已采纳

PHP和Javascript中的多个类

I'm new beginner in the php, html, css and working on a projekt(for my self). I got a form with dependent dropdown box, but i'm not allow to assign multiple classes, because it making conflict with my javascript. I would like to use the same class to keep my form similar. What i can do? if i choose to use multiple classes and then my javascript wouldn't work currecly. I got to dropdown box called "category" and "task_type" and i would like to use "form-control" class to keep my form similar in the javascript, i'm not allow to use form-control to times.

source code: https://www.allphptricks.com/dynamic-dependent-select-box-using-jquery-and-ajax/

Category: <select name="category" class="form-control" required>
    <option value="0">Select Category</option>
    <?php
    include('dbconnect.php');
    $sql = mysqli_query($DBcon,"SELECT * FROM category");
    while($row=mysqli_fetch_array($sql))
    {
        echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
    } ?>
</select>
<br/><br/>
Task Type:<select name="task_type" class="form-control" required>
    <option>Select Task Type</option>
</select>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function()
    {
        $(".form-control").change(function()
        {
            var category_id=$(this).val();
            var post_id = 'id='+ category_id;

            $.ajax
            ({
                type: "POST",
                url: "ajax.php",
                data: post_id,
                cache: false,
                success: function(cities)
                {
                    $(".form-control").html(cities);
                }
            });

        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • dongzhen4180 2018-08-14 00:23
    关注

    You can't use the form-control class for this, because you don't want the same action for both dropdowns. Give them IDs to distinguish them, and use the IDs in your selectors.

    Category: <select id="category" name="category" class="form-control" required>
        <option value="0">Select Category</option>
        <?php
        include('dbconnect.php');
        $sql = mysqli_query($DBcon,"SELECT * FROM category");
        while($row=mysqli_fetch_array($sql))
        {
            echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
        } ?>
    </select>
    <br/><br/>
    Task Type:<select id="task_type" name="task_type" class="form-control" required>
        <option>Select Task Type</option>
    </select>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
    </script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $("#category").change(function()
            {
                var category_id=$(this).val();
                var post_id = 'id='+ category_id;
    
                $.ajax
                ({
                    type: "POST",
                    url: "ajax.php",
                    data: post_id,
                    cache: false,
                    success: function(cities)
                    {
                        $("#task_type").html(cities);
                    }
                });
    
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么