JavaScript 提示框取消按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6962718/
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
JavaScript Prompt Box Cancel Button?
提问by Jason
I have a JavaScript function as follows:
我有一个 JavaScript 函数,如下所示:
function popup(username) {
var req = createAjaxObject();
var message = prompt("Message:","");
if(message != ""){
req.onreadystatechange = function() {
if (req.readyState == 4) {
alert(req.responseText);
}
}
req.open('POST','getmessage.php',true);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send("username=" + username +"&message="+message);
} else {
alert("Please enter a message");
}
}
When the Cancel
button is hit, the form is still processed through getmessage.php
. Any way to have the Cancel
button do nothing?
当Cancel
按钮被点击时,表单仍然通过 进行处理getmessage.php
。有Cancel
什么办法让按钮什么都不做?
EDIT:
编辑:
Here is the way this function is called:
下面是这个函数的调用方式:
<?php
mysqlLogin();
$username = $_COOKIE['sqlusername'];
$sql = mysql_query("SELECT username FROM `users` WHERE username!='$username'");
if(mysql_num_rows($sql) != 0) {
echo "<table class='usertable' align='center'>";
while($row = mysql_fetch_array($sql)){
$username = $row['username'];
echo "<tr><td><center>" . $row['username'] . "</center></td><td> <a href='#' onClick=\"popup('$username');\">Send Message</a></td></tr>";
}
echo "</table>";
} else {
echo "<center>No users found!</center>";
}
?>
The PHP
script its linked to:
该PHP
脚本链接到:
<?php
$id = rand(1,1500);
$poster = $_POST['username'];
$message = $_POST['message'];
$to = $_COOKIE['sqlusername'];
require('functions.php');
mysqlLogin();
$sql = mysql_query("INSERT INTO `messages` VALUES ('$id','$message','$to','$poster','')");
if($sql){
echo "Message sent!";
} else {
echo "Woops! Something went wrong.";
}
?>
回答by pimvdb
In the case of Cancel
, the prompt result is null
, and null != ''
(as per ECMA-262 Section 11.9.3).
在 的情况下Cancel
,提示结果为null
, 和null != ''
(根据 ECMA-262 第 11.9.3 节)。
So, add an extra explicit check for null
inequality:
因此,为null
不等式添加额外的显式检查:
if(message != "" && message !== null) {
However, since the message is either some string or null
and you only want to pass when it's a string with length > 0
, you can also do:
但是,由于消息是某个字符串,或者null
您只想在它是带有 的字符串时传递length > 0
,您还可以执行以下操作:
if(message) {
This means: if message is truthy (i.e. not null
or an empty string, amongst other falsy values), then enter the if clause.
这意味着:如果消息为真(即不是null
或空字符串,以及其他假值),则输入 if 子句。
回答by user3734688
Are you using Safari by any chance? I have found that Safari seems to be returning empty string instead of null when the user clicks Cancel.
您是否有机会使用 Safari?我发现当用户单击取消时,Safari 似乎返回空字符串而不是 null。
See here: Safari 5.1 prompt() function and cancel.
回答by Paul Carroll
回答by Hackerman
var message = prompt("Message:","");
if(message){
alert("Message accepted, now i can process my php or script and blablabla!");
} else {
alert("Cancel Press or Empty Message, do nothing!");
}
回答by zus
var message = prompt('type any...', '');
if(message+'.' == 'null.')
{
alert("you've canceled");
}
else
{
alert("type ok");
}
回答by Hendro
$.messager.prompt('Save To File', 'FileName:', function(e){
if (e.response!='undefined'){
if (r!="")
{
alert('Your FileName is:' + r);
}
else
{
$.messager.alert('Err...','FileName cannot empty!!!');
}
}
});