【问题标题】:Ajax change(function() not triggeredAjax 更改(函数()未触发
【发布时间】:2014-12-15 15:01:31
【问题描述】:

我正在尝试为产品创建 3 个下拉菜单,类别 - 子类别 - 子子类别 例如:类别-健康、美容和个人==> 子类别-化妆 ==> 子子类别-眼线笔。主类别正确填充,子类别应根据用户选择填充,如果他们选择健康,美容个人护理,则应填充子类别。我编写了一个 ajax 更改函数来动态地从用户那里获取值并重新加载页面。但是,它正在接收来自用户的值,但未填充子类别。在下面的编码 sn-p 中,如果 cat_id 改变了,那么这个函数就会被触发。但是,即使用户在主类别中选择了一个选项,cat_id 也不会收到任何值。请帮助我或建议我该怎么做才能解决此问题....

$(document).ready(function(){

 $('#cat_id').change(function() 
 {

        var categoryId = $(this).val();
       // alert(categoryId);


   $.post("<?php echo DEFAULT_URL ?>/addProduct/ajax_getSubCategory", {categoryId: categoryId}, function(data) 

   {

       if (data) {

          // $('#cat_id').html(data);

           $('#subcat_id').html(data);

           $('#subsubcat_id').html('<option>Select Sub Sub Category</option>');

                 }

    });

});

});

【问题讨论】:

    标签: javascript php ajax


    【解决方案1】:

    这可能是问题的根源:

    变化:

    var categoryId = $(this).val();
    

    到:

    var categoryId = $(this).find(':selected').val();
    

    目前您正在尝试获取下拉菜单的值而不是其选定的值。

    【讨论】:

    • 感谢您的回复。我试过了,但它不起作用。 cat_id 没有改变。它仍然接收空值。如果 cat_id 被更改,那么只会触发更改功能。我不知道该怎么做......
    • 你能发布你的html吗?改变功能不是你唯一想要被触发的吗?
    • 我想在 cat_id 发生变化时触发 change 函数。但是,当我选择 Main category 值时,cat_id 没有收到任何值。
    • 能把selectBox方法生成的HTML贴出来吗?
    【解决方案2】:

    这是我用于下拉表单的 html 代码...
    /addProduct' enctype='multipart/form-data'>

                                                    <fieldset>
    
                                                        <legend>Category Information</legend>
    
                                                        <div class="required">
    
                                                            <label for="category">Main Category
    
                                                                <font color="red" title="This field is required for registration.">*</font></label>
    
                                                            <?php if(isset($merchantData->membership_package) && $merchantData->membership_package==2)
    
                                                                $categoryId=1;
    
                                                            if(isset($categoryId) && $categoryId!='')
    
                                              $whereCondition="category_id='".$categoryId."' && is_active='1' && parent_id='0' order by category_name";
    
                                                            else
    
                                                                $whereCondition="is_active='1' && parent_id='0' order by category_name";
    
                                                            ?>
    
                                                            <?php
    
                                                            echo $obj_category->selectBox('cat_id', 'Select Category', 'category_id', 'category_name', isset($cat_id) ? $cat_id :"",$whereCondition, 'class="validate[required]"');
    
    
                                                            ?>   
    
    
    
                                                        <div class="clear"></div>
    
                                                        <small>Please select the best combination of category and sub-categories for your product</small>
    
                                                        </div>
    
                                                        <div class="required">
    
                                              <label for="sub_category">Sub Category<font color="red" title="This field is required for registration.">*</font></label>
    
                                                            <?php
    
    
                                                            if (isset($cat_id) && !empty($cat_id)) {
    
    
    
                                                         echo $obj_category->selectBox('subcat_id', 'Select Sub Category', 'category_id', 'category_name', 
    

    isset($subcat_id) ? $subcat_id : '', "is_active = '1' and parent_id ='" 。 $cat_id 。 "' order by category_name", 'class="validate[required]"');

                                                            } else {
    
                                                                ?>
    
                                                                <select name="subcat_id" id="subcat_id" title="Sub Category" class="validate[required]">
    
                                                                    <option>Select Sub Category</option>
    
                                                                </select>
    
                                                            <?php } ?> 
    
                                                            <div class="required">
    
                                                                <label for="sub_sub_category">Sub Sub Category<font color="red" title="This field is required for registration.">*</font></label>
    
                                                                <?php
    
    
                                                                if (isset($subcat_id) && !empty($subcat_id)) {
    
                                                                    echo $obj_category->selectBox('subsubcat_id', 'Select Sub Sub Category', 'category_id', 'category_name', isset($subsubcat_id) ? $subsubcat_id : '', "is_active = '1' and parent_id ='" . $subcat_id . "' order by category_name", 'class="validate[required]"');
    
                                                                } else {
    
                                                                    ?>
    
                                                                    <select name="subsubcat_id" id="subsubcat_id" title="Sub Sub Category" class="validate[required]">
    
                                                                        <option>Select Sub Sub Category</option>
    
                                                                    </select>
    
                                                                <?php } ?> 
    
                                                            </div>
    
                                                    </fieldset>
    

    【讨论】:

    • 您应该删除它并使用更新的代码编辑您的原始问题
    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2015-12-25
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多