Reputation: 89
How do i use jquery ajax .load function for dropdownlist?
this is my php page
<?
$query="SELECT Name FROM supplier";
$result=mysql_query($query);
?>
<select name="ddlSupplier">
<option>Supplier Name</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['Name']?></option>
<? } ?>
</select>
how do i get value after load?
$("#ddlSupplier").val()? this.val()?
Upvotes: 0
Views: 1387
Reputation: 60594
I don't have your markup on which you call .load()
, so I'm gonna use #placeholder
as the selector to which you will be injecting the dropdown:
$('#placeholder').load('yourphppage.php', function(html) {
$(this).find('select[name=ddlSupplier]').val();
});
Note that the callback is called after HTML insertion, so you can do find()
. Docs:
If a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed. The callback is fired once for each element in the jQuery collection, and this is set to each DOM element in turn.
Upvotes: 1
Reputation: 5349
A couple of problems:
First in the PHP, you are trying to select the dropdown via an ID
but you aren't setting one. So that's added. Also, you aren't actually providing a value to the <option>
's, so that's added too.
<?
$query="SELECT Name FROM supplier";
$result=mysql_query($query);
?>
<select id="ddlSupplier" name="ddlSupplier">
<option>Supplier Name</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value="<?=$row['Name']?>"><?=$row['Name']?></option>
<? } ?>
</select>
Finally, in the JS, you need to get the value of the selection option, rather than the value of the select
list (as the select
doesn't have a value).
$("#ddlSupplier option:selected").val()
Upvotes: 0
Reputation: 8179
You should be able to get it as you said with .val()
$(function () {
$("#ddlSupplier").on('change', function () {
var value = $(this).val();
});
});
Upvotes: 0