Jump to content

if problem with append function


yami007

Recommended Posts

I'm really not so good with ajax, I just need this IF condition to work.

<script>
$(document).ready(function() {
	$.ajax({
		url: 'layouts/cities-list.php', 
		data: 'debut', 
		dataType: 'json', 
		success: function(json) {
			       $.each(json, function(index, value) { 
				$('#country').append('<option value="'+ index +);
				if (index == 144)
				{
					$('#country').append(' selected="selected"');
				}
				$('#country').append('>'+ value +'</option>');
			});
		}
	});
});
</script>

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/268046-if-problem-with-append-function/
Share on other sites

Yes there is an error, I forgot to do this:

.append('<option value="'+ index +'"');

but that doesnt solve it. This is what it ouputs:

<option select="" <="" value="1"></option>
>Afghanistan
<option select="" <="" value="193"></option>
>Afrique du Sud
<option select="" <="" value="2"></option>
>Albanie
<option select="" <="" value="3"></option>
>Alg?rie
<option select="" <="" value="81"></option>
>Allemagne

 

So there are still other errors. right?

 

Thanks, alright I solved it!

 

here is my new code:

<script>
$(document).ready(function() {
	var country_list;
	$.ajax({
		url: 'layouts/cities-list.php', 
		data: 'debut', 
		dataType: 'json', 
		success: function(json) {
			$.each(json, function(index, value) { 
				country_list = '<option value="'+index+'"';
				if ( index == 144 )
					country_list += ' selected="selected"';
				country_list += '>'+value+'</option>';
				$('#country').append(country_list);
			});
		}
	});
});
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.