Ajax更改(函数()未触发

时间:2014-10-20 15:06:00

标签: javascript php ajax

我正在尝试为产品,类别 - 子类别 - 子子类别创建3个下拉菜单 例如:category-health,beauty and personal ==> subcategory-makeup ==> subsubcategory-眼线笔。主要类别填充正确,子类别应根据用户选择填充,如果他们选择健康,美容个人护理,那么它应填充子类别。我编写了一个ajax更改函数来动态地从用户获取值并重新加载页面。但是,它正在从用户接收值但未填充子类别。在以下编码片段中,如果更改了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>');

                 }

    });

});

});

2 个答案:

答案 0 :(得分:0)

这可能是问题的根源:

更改:

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

到:

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

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

答案 1 :(得分:0)

这是我用于下拉表格的HTML代码...
/ addProduct'enctype ='multipart / form-data'&gt;

                                                <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'和parent_id ='”。 $ cat_id。 “'按类别排序”,“class =”验证[必需]“');

                                                        } 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>