JavaScript提示框取消按钮?

时间:2011-08-05 21:13:51

标签: javascript prompt

我有一个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");
}
}

点击Cancel按钮后,表单仍会通过getmessage.php进行处理。任何方法让Cancel按钮什么都不做?

修改

以下是调用此函数的方式:

                <?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>&nbsp;&nbsp;<a href='#' onClick=\"popup('$username');\">Send Message</a></td></tr>";
            }
            echo "</table>";
        } else {
            echo "<center>No users found!</center>";
        }

    ?>

其链接到的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.";
}
?>

6 个答案:

答案 0 :(得分:19)

如果是Cancel,则提示结果为nullnull != ''(根据ECMA-262第11.9.3节)。

因此,为null不等式添加额外的显式检查:

if(message != "" && message !== null) {

但是,由于邮件是某个字符串或null,而您只想在带有length > 0的字符串时传递,您还可以执行以下操作:

if(message) {

这意味着:如果消息是真实的(即不是null或空字符串,以及其他虚假值),则输入if子句。

答案 1 :(得分:3)

您是否有机会使用Safari?我发现当用户单击“取消”时,Safari似乎返回空字符串而不是null。

见这里:Safari 5.1 prompt() function and cancel

答案 2 :(得分:2)

是的,我建议的评论确实有效

var message = prompt("Message:","");
if(message){
    alert("Not working!");
} else {
    alert("Working!");
}

JSFiddle

答案 3 :(得分:1)

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!");
}

答案 4 :(得分:0)

var message = prompt('type any...', '');
if(message+'.' == 'null.')
{
    alert("you've canceled");
}
else
{
    alert("type ok");
}

答案 5 :(得分:0)

                $.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!!!');
                        }
                    }
                });