[英]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. 希望对您有所帮助。
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.