2009-12-27 8 views
-5
function SelectDistrict(argument) 
{ 
var sel=document.getElementById("city"); 
sel.style.display = ''; 
sel.options.length = 0; 

sel.options.add(new Option('Please select a location','')); 

var i=1; 
var tempInt=parseInt(argument); 
if (tempInt%10000==0) 
{ 
var place1=document.getElementById('place1'); 
place1.innerHTML =county[tempInt]; 
} 

sel.options.add(new Option('all',tempInt)); 

$('#inputcity').hide(); 
while (i<=52) 
{ 
    tempInt+=100; 

    if (county[tempInt]==undefined) 
    { 
     break; 
    } 
    else { 

    op_cir = new Option(county[tempInt], tempInt); 
      sel.options.add(op_cir); 

    } 
i++;  
    } 

} 

Répondre

2

Vous pouvez faire quelque chose comme ceci:

function SelectDistrict(argument) 
{ 
    var sel = $('#city'); // Store the jQuery object to save time 
    sel.hide().empty().append('<option>Please select a location.</option'); 
    var i = 1; 
    var tempInt = argument; 
    if (tempInt % 10000 == 0) 
    { 
     $('#place1').html(county[tempInt]); 
    } 
    sel.append('<option value="'+tempInt+'">all</option>'); 
    $('#inputcity').hide(); 
    var optionsValue = ''; // Appending strings to each other is faster than modifying the DOM 
    tempInt += 100; 
    while ((i <= 52) && (county[tempInt] != undefined)) // Just put the condition here instead of breaking the loop 
    { 
     optionsValue += "<option value='" + tempInt + "'>" + county[tempInt] + "</option>"; 
     tempInt += 100; 
     i++; 
    } 
    sel.append(optionsValue); 
} 

J'espère que cela fonctionne pour vous!

0

vous devez remplacer chaque document.getElementById() par $ ("# elementid") comme $ ("# city");

et place1.innerHTML = comté [tempInt]; par $ ("# place1"). text (comté [tempInt]);

0

Vous pouvez changer la boucle while pour:

$.each(county, function(i, itm) { 
    optionsValue += "<option value='" + i + "'>" + itm + "</option>"; 
}) 
Questions connexes