Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过AJAX弹出一个简单的警报框?_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Javascript 如何通过AJAX弹出一个简单的警报框?

Javascript 如何通过AJAX弹出一个简单的警报框?,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,以下是我在标签中的代码: <script> function saveQuantity(quantity) { $.ajax({ alert(quantity); }) } </script> $.ajax({ url: "http://yoururl.com", beforeSend: function( xhr ) { alert("if you want to call alert before request put it here and c

以下是我在标签中的代码:

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

  alert(quantity);

  })
}
</script>
$.ajax({
url: "http://yoururl.com",
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");
  }
 });
这是我的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>
$.ajax({
url: "http://yoururl.com",
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。按如下所示更改脚本

<script>
    function saveQuantity(quantity) 
    {
        alert(quantity);
    }
</script>
$.ajax({
url: "http://yoururl.com",
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,但我想您需要进行ajax调用并发出成功警报

$.ajax({
url: "http://yoururl.com",
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");
  }
 });
如果您只想发出警报,这将实现以下功能:

<script>
function saveQuantity(quantity){
alert(quantity);
}
</script>
$.ajax({
url: "http://yoururl.com",
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调用并在成功时发出警报,请执行以下操作:

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

  });
}
</script>
$.ajax({
url: "http://yoururl.com",
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");
  }
 });

您需要在executing_script.php上回显更新的数量,以便在警报中显示它

AJAX用于异步HTTP请求。看起来AJAX不是您所需要的

$.ajax({
url: "http://yoururl.com",
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");
  }
 });
如果您试图显示一个简单的警报,请执行此操作

function saveQuantity(quantity) 
{
    window.alert(quantity);
}
$.ajax({
url: "http://yoururl.com",
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方法中工作

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

})
} 
</script>
$.ajax({
url: "http://yoururl.com",
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来完成你正在做的事情,但是如果你正在研究,那么你可以像这样使用它

$.ajax({
url: "http://yoururl.com",
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");
  }
 });