admin管理员组

文章数量:1291349

Here's my code in the tag:

<script>
function saveQuantity(quantity) 
{
  $.ajax({

  alert(quantity);

  })
}
</script>

and here's my HTML:

<form name="frmQuantity">
 <select name="owned" id="owned" onChange="saveQuantity(this.value);">
  <option selected="selected" value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9+">9+</option>
 </select>
</form>

Why isn't this working? What am I doing wrong?

Here's my code in the tag:

<script>
function saveQuantity(quantity) 
{
  $.ajax({

  alert(quantity);

  })
}
</script>

and here's my HTML:

<form name="frmQuantity">
 <select name="owned" id="owned" onChange="saveQuantity(this.value);">
  <option selected="selected" value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9+">9+</option>
 </select>
</form>

Why isn't this working? What am I doing wrong?

Share Improve this question asked Nov 30, 2014 at 2:02 Ethan AllenEthan Allen 14.8k25 gold badges114 silver badges205 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

For what you are writing you don't need ajax, but I guess you want to do an ajax call and on success alert.

If you only want to alert, this will do the trick:

<script>
function saveQuantity(quantity){
alert(quantity);
}
</script>

To do an ajax call and alert on success, do it like this:

<script>
function saveQuantity(quantity) 
{
  $.ajax({
  url:"/path/to/executing_script.php",
  type:"POST",
  data:{quantity : quantity}
  success:function(data){
  alert(data);
  }

  });
}
</script>

You need to echo the updated quantity on the executing_script.php to show it in your alert

You don't need ajax for a simple alert box. Change your script as below

<script>
    function saveQuantity(quantity) 
    {
        alert(quantity);
    }
</script>

Working demo: http://jsfiddle/jo8bmqv2/

Though you don't need Ajax for the same you are doing but still if you are researching then you can use it like.

$.ajax({
url: "http://yoururl.",
beforeSend: function( xhr ) {
alert("if you want to call alert before request put it here and can skip your URL");
}
})
.done(function( data ) {
   alert("if you find alert after success response");
  }
 });

AJAX is for asynchronous HTTP request (Jquery AJAX). It doesn't look like AJAX is what you need.

If you're trying to display a simple alert do this...

function saveQuantity(quantity) 
{
    window.alert(quantity);
}

write like this it will work in ajax method

<script>
function saveQuantity(quantity)  
{
 var that=this;
 $.ajax({
 alert(that.quantity);

})
} 
</script>

本文标签: javascriptHow do I pop a simple alert box via AJAXStack Overflow