简体   繁体   English

如何在JavaScript的确认功能中添加字符串值

[英]How to add string value in confirm function of JavaScript

I have JavaScript function in my asp button. 我的asp按钮中有JavaScript函数。

<asp:Button ID="btnCommission" runat="server" Text="Deposit Commission" OnClientClick="if (!confirm('Are you sure you want to deposit commission into points?')) return false;" OnClick="btnCommission_Click" />

but I want to value <%= (commission_total)%> in my confirm function like this : 但是我想在我的确认函数中将<%= (commission_total)%>为:

OnClientClick="if (!confirm('Are you sure you want to deposit' + <%= (commission_total)%> + 'commission into points?')) return false;"

but it doesn't allow me to add this value saying "this is not scriptlet". 但不允许我添加此值并说“这不是脚本”。 But I want to confirm how much they are going to deposit. 但我想确认一下他们要存多少钱。 How can I do that? 我怎样才能做到这一点?

You could grab the value the user entered from wherever they entered it using JavaScript. 您可以从使用JavaScript的任何地方输入用户输入的值。 Let's say the user's entering the value into some textbox that looks like this: <asp:Textbox runat="server" ID="myAmount" /> . 假设用户正在将值输入到如下所示的某些文本框中: <asp:Textbox runat="server" ID="myAmount" /> We could then grab the value like this: document.querySelector('input[id$="myAmount"]').value . 然后,我们可以像这样获取值: document.querySelector('input[id$="myAmount"]').value So, you could use this as follows: 因此,您可以按以下方式使用它:

OnClientClick="if (!confirm('Are you sure you want to deposit' + document.querySelector('input[id$="myAmount"]').value + 'commission into points?')) return false;"

This is one of the possible solutions. 这是可能的解决方案之一。 Because the OnClientClick event happens before postback, you'll have to do this in JavaScript. 由于OnClientClick事件发生在回发之前,因此您必须使用JavaScript进行此操作。 If you don't want to do this in JavaScript, you'll have to let postback happen and handle open a popup to confirm the amount. 如果您不想在JavaScript中执行此操作,则必须让回发发生并处理一个弹出窗口以确认金额。 If you want to do it this way, you might look into using the Ajax Toolkit or something similar to open a window after the user clicks the button. 如果您想以这种方式进行操作,则可以考虑使用Ajax Toolkit或类似的方法在用户单击按钮后打开一个窗口。

I hope that helps. 希望对您有所帮助。

Update 更新

You could use a hidden field and update the hidden field with the value. 您可以使用隐藏字段,并使用该值更新隐藏字段。 Then you could grab that value from JavaScript and show it in the confirm dialog. 然后,您可以从JavaScript中获取该值并将其显示在确认对话框中。 Here's the markup 这是标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function showValue() {
            var val = document.querySelector('#<%= test1.ClientID %>').value;
            return confirm('Is this correct? ' + val);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField runat="server" ID="test1" ></asp:HiddenField>
        <asp:Button runat="server" ID="tester" OnClientClick="showValue();" Text="Show" />
    </div>
    </form>
</body>
</html>

And the code behind: 以及后面的代码:

using System;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected double test;
        protected void Page_Load(object sender, EventArgs e)
        {
            test = 12.50;

            test1.Value = test.ToString();
        }
    }
}

Obviously you don't have to assign the value of the hidden field on page load. 显然,您不必在页面加载时分配隐藏字段的值。 You can do it wherever makes the most sense for the site you're building. 您可以在最适合您所建网站的地方进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript确认值不起作用 - javascript confirm value not working 如何在C#.net中获取JavaScript确认弹出窗口值 - How to get javascript confirm popup value in c#.net 如何从asp.net中的scriptmanager.registerclientscript中写的javascript确认框返回值。 - How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.net.? 如何为Dictionary添加onclick javascript <string, string> ? - how to add onclick javascript for Dictionary<string, string>? 如何添加功能以将空值添加到组合框 - How to add function to add null value to combobox 在C#中接受Javascript确认框的返回值 - Accepting return value of Javascript Confirm box in C# JavaScript确认错误值后,Response.Redirect()无法正常工作 - Response.Redirect() not working after javascript confirm got false value "<i>How to add a string to a string[] array?<\/i>如何将字符串添加到 string[] 数组?<\/b> <i>There&#39;s no .Add function<\/i>没有 .Add 功能<\/b>" - How to add a string to a string[] array? There's no .Add function 写一个函数来创建一个字符串(JavaScript),然后将其添加到按钮作为其属性? - writing a function which creates a string (javascript) and then add it to a button as its attribute? 如何从代码隐藏向HmlGenericControl添加JavaScript函数 - How to add a JavaScript function to an HmlGenericControl from codebehind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM