I have a product page with an add to cart button
我有一个添加购物车按钮的产品页面
When the add to cart button is clicked, a couple of input fields are validated using ASP.NET validation controls (which use client-side validation to prevent the postback from occurring upon invalid input).
单击add to cart按钮时,将使用ASP对几个输入字段进行验证。NET验证控件(使用客户端验证来防止在无效输入时发生回发)。
In order to prevent double-clicks from adding multiple items to the cart, I would like to disable the add to cart button after the initial click, but ONLY if validation has succeeded. (It would be rather bad to disable "add to cart" just because they goofed up an input)
为了防止双击将多个项目添加到购物车中,我希望在初始单击之后禁用add to cart按钮,但前提是验证成功。(仅仅因为输入出错就禁用“添加到购物车”会很糟糕)
Is it possible to handle the "on ASP.NET client-side validation succeeded" event in javascript?
是否有可能处理“on ASP”。NET客户端验证成功“事件在javascript?
I may just end up rolling my own validation mechanism for this page.
我可能最终会为这个页面运行我自己的验证机制。
2 个解决方案
#1
2
Just an example, but you can adopt this to your needs:
这只是一个例子,但你可以根据自己的需要:
Markup:
标记:
<asp:button id="bla" runat="server" Text="bla" onClientClick="ValidationCatcher()" />
Script:
脚本:
function ValidationCatcher()
{
//force .net validation
Page_ClientValidate();
for(i=0; i < Page_Validators.length; i++)
{
if(!Page_Validators[i].isvalid)
{
//do whatever you need to do
}
}
}
#2
1
Building on rick schott's answer to hide the button once validation succeeds, & reshow the button if the user hits escape:
基于rick schott的回答,验证成功后隐藏按钮,如果用户点击escape,则重新显示按钮:
<asp:Button ID="btnProcess" runat="server" Text="Donate" OnClick="BtnProcessClick" ValidationGroup="donate" ClientIDMode="Static" onClientClick="HideBtnProcess()">
<div id="pWait" >Please wait...</div>
<script type="text/javascript">
$(document).ready(function() {
$("#pWait").hide();
});
function HideBtnProcess() {
if (Page_ClientValidate("donate")) {
$("#btnProcess").hide();
$("#pWait").show();
$(document).keyup(function (event) {
if (event.which === 27) {
$("#btnProcess").show();
$("#pWait").hide();
}
});
}
}
#1
2
Just an example, but you can adopt this to your needs:
这只是一个例子,但你可以根据自己的需要:
Markup:
标记:
<asp:button id="bla" runat="server" Text="bla" onClientClick="ValidationCatcher()" />
Script:
脚本:
function ValidationCatcher()
{
//force .net validation
Page_ClientValidate();
for(i=0; i < Page_Validators.length; i++)
{
if(!Page_Validators[i].isvalid)
{
//do whatever you need to do
}
}
}
#2
1
Building on rick schott's answer to hide the button once validation succeeds, & reshow the button if the user hits escape:
基于rick schott的回答,验证成功后隐藏按钮,如果用户点击escape,则重新显示按钮:
<asp:Button ID="btnProcess" runat="server" Text="Donate" OnClick="BtnProcessClick" ValidationGroup="donate" ClientIDMode="Static" onClientClick="HideBtnProcess()">
<div id="pWait" >Please wait...</div>
<script type="text/javascript">
$(document).ready(function() {
$("#pWait").hide();
});
function HideBtnProcess() {
if (Page_ClientValidate("donate")) {
$("#btnProcess").hide();
$("#pWait").show();
$(document).keyup(function (event) {
if (event.which === 27) {
$("#btnProcess").show();
$("#pWait").hide();
}
});
}
}