यहाँ AJAX कॉल के साथ पूरी स्क्रिप्ट है जिसमें एक पेज को एकाधिक सूचियों के साथ एक सूची में लक्षित किया गया है। मेरे लिए ऊपर दिए गए अन्य सामान में से किसी ने भी तब तक काम नहीं किया जब तक कि मैंने "id" विशेषता का उपयोग नहीं किया, हालांकि मेरा विशेषता नाम "ItemKey" है। डिबगर का उपयोग करके
क्रोम डीबग
मैं यह देखने में सक्षम था कि चयनित विकल्प में विशेषताएं थीं: JQuery के "आईडी" और मूल्य के लिए एक मानचित्र के साथ।
<html>
<head>
<script type="text/JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<select id="List1"></select>
<select id="List2">
<option id="40000">List item #1</option>
<option id="27888">List item #2</option>
</select>
<div></div>
</body>
<script type="text/JavaScript">
//get a reference to the select element
$select = $('#List1');
//request the JSON data and parse into the select element
$.ajax({
url: 'list.json',
dataType:'JSON',
success:function(data){
//clear the current content of the select
$select.html('');
//iterate over the data and append a select option
$.each(data.List, function(key, val){
$select.append('<option id="' + val.ItemKey + '">' + val.ItemText + '</option>');
})
},
error:function(){
//if there is an error append a 'none available' option
$select.html('<option id="-1">none available</option>');
}
});
$( "#List1" ).change(function () {
var optionSelected = $('#List1 option:selected').attr('id');
$( "div" ).text( optionSelected );
});
</script>
</html>
यहाँ JSON फ़ाइल बनाने के लिए है ...
{
"List":[
{
"Sort":1,
"parentID":0,
"ItemKey":100,
"ItemText":"ListItem-#1"
},
{
"Sort":2,
"parentID":0,
"ItemKey":200,
"ItemText":"ListItem-#2"
},
{
"Sort":3,
"parentID":0,
"ItemKey":300,
"ItemText":"ListItem-#3"
},
{
"Sort":4,
"parentID":0,
"ItemKey":400,
"ItemText":"ListItem-#4"
}
]
}
आशा है कि यह मदद करता है, मुझे यह प्राप्त करने के लिए उपरोक्त सभी को धन्यवाद।