javascript 如何通过 AJAX 弹出一个简单的警告框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27209245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pop a simple alert box via AJAX?
提问by Ethan Allen
Here's my code in the tag:
这是我在标签中的代码:
<script>
function saveQuantity(quantity)
{
$.ajax({
alert(quantity);
})
}
</script>
and here's my HTML:
这是我的 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?
为什么这不起作用?我究竟做错了什么?
回答by ekad
You don't need ajax for a simple alert box. Change your script as below
对于简单的警报框,您不需要 ajax。更改您的脚本如下
<script>
function saveQuantity(quantity)
{
alert(quantity);
}
</script>
Working demo: http://jsfiddle.net/jo8bmqv2/
工作演示:http: //jsfiddle.net/jo8bmqv2/
回答by baao
For what you are writing you don't need ajax, but I guess you want to do an ajax call and on success alert.
对于您正在编写的内容,您不需要 ajax,但我想您想要进行 ajax 调用并收到成功警报。
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:
要执行 ajax 调用并在成功时发出警报,请执行以下操作:
<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
您需要在 上回显更新的数量以将executing_script.php
其显示在您的警报中
回答by KanhuP2012
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 来做同样的事情,但是如果您正在研究,那么您仍然可以使用它。
$.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");
}
});
回答by user2957047
AJAX is for asynchronous HTTP request (Jquery AJAX). It doesn't look like AJAX is what you need.
AJAX 用于异步 HTTP 请求 ( Jquery AJAX)。看起来 AJAX 不是您所需要的。
If you're trying to display a simple alert do this...
如果您想显示一个简单的警报,请执行此操作...
function saveQuantity(quantity)
{
window.alert(quantity);
}
回答by yugi
write like this it will work in ajax method
像这样写它会在ajax方法中工作
<script>
function saveQuantity(quantity)
{
var that=this;
$.ajax({
alert(that.quantity);
})
}
</script>