2014-03-03 143 views
0

我有這些:如何使用下拉列表中的值來隱藏隱藏表單域?

$(document).ready(function() { 
    getRequestCategories(); 

    $('#requestCategory').change(function() { 
     getRequestDescriptions($(this).val()); 
    }); 
}); 


function getRequestCategories() { 
    $.ajax({ 
     url: 'getCategories.php', 
     dataType: 'json' 
    }) 
    .done(function(categoryInfo) { 
     $(categoryInfo).each(function(i, category) { 
      $('<option>').val(category.RequestCategoryDisplay).text(category.RequestCategoryDisplay).appendTo($('#requestCategory')); 
     }) 
    }); 
} 


function getRequestDescriptions(requestCategory) { 
    $.ajax({ 
     url: 'getDescriptions.php', 
     dataType: 'json', 
     data: { requestCategory: requestCategory } 
    }) 
    .done(function(descriptionInfo) { 
     $(descriptionInfo).each(function(i, description) { 
      $('<option>').val(description.RequestDescriptionDisplay).text(description.RequestDescriptionDisplay).appendTo($('#description')); 
     }) 
    }); 
} 



     Category 
      <select name="requestCategory" id="Category" style="width:250px;font-size:10pt;" class="changeable" data-summary="summCategory"> 
      <option value=""></option> 
      </select> 
     Description 
     <select name="description" id="description" style="width:250px;font-size:10pt;" class="changeable" data-summary="summSubCategory"> 
     <option value=""></option> 
    </select> 

它是如何工作目前:

當您選擇從Category下拉列表中值,與您的選擇相關聯的所有值都將自動進入description下拉。

這工作正常。

但是,我們有一個新的要求,從description下拉用戶的選擇來填充下面隱藏的表單字段:

<input name="RequestID" id="RequestID" type="text" width="300" value="" class="changeable" /> 

換句話說,一旦description下拉列表與值填充從Category基於選擇下拉菜單,然後當用戶從description下拉列表中選擇其中一個值時,RequestID的伴隨值應保存到隱藏表單字段中。

我試着修改getDescriptions函數,但我沒有得到正確的值。

我從描述下拉列表中選擇的每個值都給了我相同的RequestID值。

你能看到我做錯了嗎?

非常感謝。

 function getDescriptions(requestCategory) { 
      $.ajax({ 
       url: 'getDescriptions.php', 
       dataType: 'json', 
       data: { requestCategory: requestCategory } 
      }) 
      .done(function(descriptionInfo) { 

      // get number of items in array given by php 
      var Desc_count = descriptionInfo.length; 

      // loop request descriptions 
      for (var i = 0; i < Desc_count; i += 1) { 

       // append an <option> tag to your <select> 
       $('#description').append('<option value="' + descriptionInfo[i].RequestID + '">' + descriptionInfo[i].RequestDescriptionDisplay + '</option>'); 
      } 

      // Listen for the value of the <select> to change 
      $('#description').on('change', function() { 
       // get the value of the selected option ,the value is the descriptionInfo[i].RequestID 
       var value = $("#description option:selected").val(); 
       // Set the value of the hidden fields based on the <select>'s ID choosing the corret array element 
       $('input[name="RequestID"]').val(value); 
      }); 
     }); 
} 
+0

嘗試將 「更改偵聽器」 之外的功能在requestCategory更改偵聽器之後。 – Wilmer

回答

1

很奇怪這不是工作..是你的變化事件發生$(document).ready()?

我盡力幫助:

$('#description').on('change', function() {事件的instad試試這個:

$(document).ready(function() { 
    $('#description').change(function() { 
     $('#RequestID').val($(this).val()); 
    }); 
}); 

如果這does not工作嘗試:

$(document).ready(function() { 
    $('#description').live('change', function() { 
     $('#RequestID').val($(this).val()); 
    }); 
}); 
+0

我嘗試了你的兩個代碼,但仍然得到相同的東西,除非我錯誤地使用它。 威爾默,我不知道我跟着,對不起。 感謝您的回覆。 – Kenny

+0

你可以嘗試通過JavaScript檢查運行它嗎?像.. www.jslint.com。我不確定它爲什麼不能正常工作 - 您的控制檯是否顯示chrome/firefox中的任何錯誤?也許某處出現語法錯誤..如果你可以製作一個完美的小提琴。 –

+0

我不能擺弄它,因爲1,它是內部的,2)許多不同的依賴片。 但是,我檢查了它,沒有發現錯誤。 它正在產生的結果,就像我的,只是不正確的結果。 例如,有5個類別。在這些類別中,只有一個具有這樣的值:null,null,null,null,'Cataract'。 因此,第一次從沒有值的類別中選擇時,它不顯示任何內容。這很好。然後,您從具有價值的那個中選擇,它會顯示我展示的值。現在,你回到那些沒有價值的人,他們開始...... – Kenny