Reputation:
I have used the onclick() javascript event in my cake app. but that not work in google chrome and safari. here is the my code
<select name="ovwear" >
<option>--Select Category--</option>
<?php
$i = 1;
foreach($styleDetail as $key=>$val) { ?>
<option onClick="catDetail(<?php echo $val['shop_style_categories']['cat_id'].",".$val['shop_style_categories']['parent_id'] ?>);" value="<?php echo $i;?>">Category <?php echo $i;?></option>
<?php $i++; } ?>
</select>
here is the function:
<script type="text/javascript">
function catDetail(cat_id,parent_id){
//alert("called here");
var cat_id = cat_id;
var parent_id = parent_id;
jQuery.ajax({
type: "POST",
url: "/admin/shop/styledata",
data:"cat_id=" + cat_id,
dataType: "html",
success: function(data) {
$("#alertt").html(data);
}
});
}
</script>
Upvotes: 1
Views: 3009
Reputation: 268344
You're using jQuery, therefore, you should use jQuery. You should be binding to the change event of the select element itself, and storing these relevant values as data attributes:
<?php
$o = "";
for ( $i = 0; $i < count( $styleDetail ); $i++ ) {
$o .= sprintf("<option data-cat-id='%s' data-parent-id='%s'>%s</option>",
$styleDetail[$i]['shop_style_categories']['cat_id'],
$styleDetail[$i]['shop_style_categories']['parent_id'],
"Category " . $i+1 );
}
?>
<select name="ovwear"><?php echo $o; ?></select>
Next, we need to bind the data up:
<script>
$(function(){
$("select[name='ovwear']").on("change", function(){
var selected = $("option:selected", this);
$.ajax({
type: "POST",
url: "/admin/shop/styledata",
data: { 'cat_id':$(selected).data("cat-id") },
dataType: "html",
success: function(data) {
$("#alert").html(data);
}
});
});
});
</script>
Upvotes: 0
Reputation: 35572
onclick has to be on `<select` instead of `<option`
<select name="ovwear" onClick=.....
Upvotes: 3
Reputation: 8401
You should put onclick event on the select element and not on the options.
Upvotes: 0
Reputation: 119837
I think you can't attach an onclick
on <option>
. Better use onchange
on <select>
instead.
And since you are using jQuery, take advantage of using .on()
to attach handlers. Also, use data-*
attributes for custom data on tags. More details in this answer.
Upvotes: 1