Skip to content Skip to sidebar Skip to footer

Json Not Working With Onchange Event

I originally coded this piece to bring back a single data point. I realized I needed more fields returned with an onchange event (drop down select) and changed it to use JSON. It

Solution 1:

So, I have corrected your errors and you should have the below for it to work with no errors, unless there is something else that is causing errors.

<select id='catlist' name='catlist'>
    <optionvalue='NA'>Select Category</option><optionvalue='id1'>val1</option><optionvalue='id2'>val2</option><optionvalue='id3'>val3</option>
</select>

$(function(){
    $("#catlist").change(function() {
        var sVal = $(this).val();
        if (sVal !== "NA") { // I am assuming you don't want to make an ajax call when the value is 'NA'!
            $.getJSON("ajax.php", {catid: $(this).val()}, function(result) {
                alert(result); 
            });
            //by the way $(this).val() = id1|id2|id3
        }
    });
});

Solution 2:

Well if you format your code, you can see your problem.

$(document).ready(function() {
  $("#catlist").change(function() {
    var vid = document.getElementById("catlist").value;

    $.getjson("ajax.php", {
        catid: vid
      },
      function(result) {
        alert(result);
      }
      .error(function(xhr) {   //<-- error inside getJSONalert(xhr)
      });)                     //<-- a random )
  })
})

and

You have an inline event onchange='getval(this);' but you do not have a function getval listed.

Post a Comment for "Json Not Working With Onchange Event"