2014-10-20 62 views
0

我想創建一個產品,類別 - 子類別 - 子子類別 3下拉菜單爲例如:類別健康,美麗和個人==>子類別化妝= => sububcategory- Eyeliner。主要類別填充正確和子類別應根據用戶選擇填充,如果他們選擇健康,美容個人護理,然後它應該填充子類別。我寫了一個Ajax更改函數動態獲取值用戶並重新加載頁面,但是它接收到來自用戶的值,但是子類別沒有填充,在下面的代碼片段中,如果cat_id被改變,則該函數將被觸發,但是,即使用戶,cat_id也沒有收到任何值請在主要類別中選擇一個選項請幫助我或給我建議我需要做些什麼來解決這個問題......Ajax變化(功能()沒有觸發

$(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>'); 

       } 

    }); 

}); 

}); 

回答

0

這可能是問題的根源:

變化:

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

到:

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

在你正試圖獲得的價值的那一刻下拉而不是選定的值。

+0

感謝您的答覆。我試過這個,但它不工作。 cat_id不會改變,它仍然接收空值。如果cat_id被改變,那麼只有改變函數纔會被觸發。我不知道該怎麼做...... – sam 2014-10-20 15:34:20

+0

你能發佈你的html嗎?更改功能是不是你想要觸發的唯一的事情? – 2014-10-20 15:40:06

+0

我想在更改cat_id時觸發更改功能。但是,當我選擇主類別值時,cat_id沒有收到任何值。 – sam 2014-10-20 19:54:36

0

這是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'和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> 
+0

您應該刪除它並使用更新的代碼編輯您的原始問題 – 2014-10-21 08:40:13