确认Javascript中的值 [英] Confirm Value in Javascript

查看:87
本文介绍了确认Javascript中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的问题.如何从确认中获取值并使用该值做某事?

例如...

Very simple question. How do I get the value from confirm and use that value to do something?

For example...

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validate()
        {
            return confirm('Are you sure to continue?');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn1" runat="server" Text="Press Here" OnClientClick="validate()" OnClick="btn1_Click" />
        <asp:Label ID="lb1" runat="server" />
    </div>
    </form>
</body>
</html>



使用上面的代码,我收到确认消息,然后可以选择确定"或取消"

当我按OK时,我要显示来自Label的消息.

如果按CANCEL,我想显示另一条消息.



With the code above I get the confirm message and I can choose "OK" or "CANCEL"

When I press OK, I want to display a message from Label.

If I press CANCEL, I want to display another message.

public partial class JavascriptConfirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn1_Click(object sender, EventArgs e)
    {
        lb1.Text = "You Pressed OK";
    }
}



使用上面的代码,无论我从确认消息中选择什么,它都只会显示"You Pressed OK".

任何人都可以帮忙吗?



With the code above ofcourse it will display only "You Pressed OK" no matter what I choose from confirm message.

Can anyone help?

推荐答案

您可以使用以下代码.

You can use the following code.

var str="MyValue";
if(confirm("Click OK to Proceed")==true) //If OK then TRUE
{
    alert("Your OK message"+str)
}
else
{
    alert("Your Cancel message"+str)
}


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
     function validate()
      {
        if(confirm(''Are you sure to continue?''))        
             document.getElementById(''hdnConfirmResult'').value=''true'';         
        else         
             document.getElementById(''hdnConfirmResult'').value=''false'';        
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:hiddenfield id="hdnConfirmResult" runat="server" value="" xmlns:asp="#unknown" />
        <asp:Button ID="btn1" runat="server" Text="Press Here" OnClientClick="validate()" OnClick="btn1_Click" />
        <asp:Label ID="lb1" runat="server" />
    </div>
    </form>
</body>
</html>


然后在CodeBehind中:


Then in CodeBehind:

protected void btn1_Click(object sender, EventArgs e)
 {
     bool Confirmed= Convert.ToBoolean(hdnConfirmResult.value);//can cause exception incase the value is not true or false
     if(bConfirmed)
        lb1.Text = "You Pressed OK";
     else
        lb1.Text="";//Whatever you want to display
 }


虽然我不明白为什么您要回发到服务器,以防用户选择取消.如果只是显示此消息,则说明您做错了.您可以从客户端使用javascript本身显示此消息.


Although i dont understand why you want to postback to the server in case the user chose to cancel. If it is just to display this message then you are doing it wrong. you can display this message using javascript itself from clientside.




您可以将另一个隐藏按钮用作选项编号,例如,

Hi,

you can put another hidden button for the option No. say,

<asp:Button id="btnNo" runat="server" onclick="btnNo_Click" style="visibility:hidden;" />



并且在js代码中,当用户在确认"对话框中单击否"时,您已经触发了否"按钮的点击,并返回false来取消当前按钮的点击.

请参见下面的代码:



And in the js code, when user clicks on no in confirm dialog, you have trigger the no button''s click and return false to cancel current button''s click.

See the Code below:

if(confirm("Click OK to Proceed")==true) //If OK then TRUE
{
        return true;
}
else
{
    document.getElementById('btnNo').click();
    return false;
}




并且在文件后面的代码中,您必须为btn编写click事件处理程序,方法与btn1相同.




And in code behind file you have to write click event handler for btnNo same way as btn1.

protected void btnNo_Click(object sender, EventArgs e)
    {
        lb1.Text = "You Pressed Cancel";
    }




希望对您有帮助:)




Hope this helps you :)


这篇关于确认Javascript中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆