简体   繁体   中英

On change event won't fire select list

I can't seem to get jQuery to fire and on change event for the county_id select list. I have tried multiple code snippets and none will fire the on change. Any ideas?

HTML:

<select id="county_id" name="county_id">
    <option value=""></option>
    <option value="1">Somewhere</option>
    <option value="2">Somewhere else</option>
</select>

<select id="vice_county_id" name="vice_county_id"></select>

JS:

$(function(ready) {
    $("#county_id").change(
            function() {
                $("#vice_county_id").html("");
                var co_id = $("#county_id > option[@selected]").attr("value");
                if (co_id != 0) {
                    $.getJSON('./vice_county_web_service.php?co_id=' + co_id,
                            function(data) {
                                $.each(data, function() {
                                    $("#vice_county_id").append(
                                            $("<option></option>").val(
                                                    this['value']).html(
                                                    this['name']));
                                });
                            });
                }
            });

});

just enough to get value of select box

var co_id = $("#county_id").val();

You should use on()

$('#county_id').on('change', function() {
  //....
  var co_id = $("#county_id").val(); 
});

try:

$(document).ready(function(){
    $("#county_id").change(function() {
        $("#vice_county_id").html("");
        var co_id = $(this).val();
        if(county_id != 0) {
            $.getJSON('./vice_county_web_service.php?co_id=' + co_id,function(data) {   
                $.each(data, function() {
                    $("#vice_county_id").append($("<option></option>").val(this['value']).html(this['name']));
                });
            });
        }
    });
})

Change `var co_id = $("#county_id > option[@selected]").attr("value");' to 'var co_id = $("#county_id > option:selected").attr("value");'

http://jsfiddle.net/XvRsV/

The event is firing, but this line of code is failing:

var co_id = $("#county_id > option[@selected]").attr("value");

...because that's an invalid selector. In your browser's JavaScript console, you should be seeing Syntax error, unrecognized expression: [@selected] .

jQuery will happily get you the value, just use val :

var co_id = $("#county_id").val();

Live example | source

Your code is working fine means the change event is getting handled by the code properly Please refer this link

$(function(ready){
            $("#county_id").change(function() {
                $("#vice_county_id").html("test");
                var co_id= $("#county_id").attr("value");
                alert('Value : ' +co_id); 
            if(co_id != 0) {
                $.getJSON('./vice_county_web_service.php?co_id=' + co_id,function(data) {    
                    $.each(data, function() {
                        $("#vice_county_id").append($("<option></option>").val(this['value']).html(this['name']));
                    });
                });
            }
        });

    });​

Change this line only,

var co_id= $("#county_id").attr("value");

Just check if your ajax call is working or not

 <select id="county_id" name="county_id" onchange="ChangeCountry();">

function ChangeCountry()
{
   var _countryId = $('#county_id').val();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM