我有用JQuery生成的下拉(選擇)菜單,動態使用來自PHP腳本的JSON。 請參閱附件中的圖片,在組件標籤下。 「選擇」菜單包含組件名稱和組件ID(值)。 更改JQuery動態下拉菜單填充其他字段
我想要使用JQuery的「更改」事件來填充以下字段代碼,類別和UOM,以及相應的值。
我的JSON對象檢索所需的一切(我設法用JQuery自動完成來實現這一點)。
例如。
[{"id":"4","component":"Component 1","code":"COMP1","uom":"kilo","category":"Flour"}...]
這是我的代碼,用於生成「選擇」菜單,其中包含來自上述JSON的選項。
$.getJSON("<?php echo site_url('products/dropdown/no'); ?>", function(result) {
var optionsValues = '<select>';
optionsValues += '<option value="">' + '--' + '</option>';
$.each(result, function() {
optionsValues += '<option value="' + this.id + '">' + this.component + '</option>';
});
optionsValues += '</select>';
var options = $("select[name=component]");
options.replaceWith(optionsValues);
});
它位於$(document).ready中。
所以,我需要功能或者什麼東西,只要在「選擇」菜單發生變化時,它就會用相應的值填充所提到的字段。
非常感謝您提前。
如何指的是與所選擇的下拉值對應的那些特定的值(代碼,類別,計量單位)? – 2011-04-21 13:00:37
在你的getJSON函數中,你可以使用result.code,result.category等獲取你的php腳本中的值,然後你必須使用$('input [name = code]').val(result.code)或者無論什麼領域被稱爲添加它。 – Calum 2011-04-21 20:56:45