Javascript 如何使用“是”或“否”按钮获取OnClientClick Return的值

Javascript 如何使用“是”或“否”按钮获取OnClientClick Return的值,javascript,asp.net,Javascript,Asp.net,我知道如何让返回确认框在按钮点击时弹出,但在我的情况下,我只想知道用户点击了哪个按钮是或否。一旦客户端点击AddToCartBtn,我会将它们重定向到另一个url。我希望他们被重定向到URL,无论他们点击什么,但返回确认后,它将在点击否时取消重定向。在这种情况下,我是否应该使用确认以外的内容来接收用户选择的值,然后继续将他们重定向到新页面?到目前为止,我仅有的代码只是用于简单的确认弹出窗口 <asp:ImageButton ID="AddToCartBtn" runat="server"

我知道如何让返回确认框在按钮点击时弹出,但在我的情况下,我只想知道用户点击了哪个按钮是或否。一旦客户端点击AddToCartBtn,我会将它们重定向到另一个url。我希望他们被重定向到URL,无论他们点击什么,但返回确认后,它将在点击否时取消重定向。在这种情况下,我是否应该使用确认以外的内容来接收用户选择的值,然后继续将他们重定向到新页面?到目前为止,我仅有的代码只是用于简单的确认弹出窗口

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>'
ImageUrl="~/Pictures/ShoppingCart.png"                                          
OnClick="AddToCartBtn_Click" OnClientClick="return confirm('are you sure?')" />
虽然我在javascript中找到了一个自定义弹出窗口,其中包含一个复选框,只要我能得到值,复选框控件就可以取代是或否按钮,但我无法找到更多其他内容,我也将包含此代码,以防这是我应该走的方向

<script type="text/javascript">
$('AddToCartBtn').click(function() {
var answer = $("#dialog").dialog()
    .find(':checkbox').unbind('change').bind('change', function(e){
    if(this.checked) alert('checked box');
    else alert('unchecked box');
});
})
</script>

<button>CLICK ME</button>

<div id='illegal'></div>

<div id='numberplateyellow'></div>

<div id='numberplatewhite'></div>

<div id='dialog'>
The registration you have entered is illegal for the uk roads. By clicking ok you are accepting          full resposibility for this plate and agreeing to use it for offroad use only.
<br/>
  Check here if you agree to the terms and conditions: <input type='checkbox'/>
</div>

我不习惯在asp.net中使用javascript,因此我不确定如何在按钮的onClick事件中调用此javascript。javascript代码下面的按钮和容器是来自一个站点的测试示例,但在我的例子中,我需要按钮是我的asp:ImageButton AddToCartBtn,而不是html按钮。那么,总而言之,有没有一种方法可以使用返回确认弹出窗口并获取用户单击的值,但如果他们单击否,仍然将其重定向到页面?或者我应该更倾向于自定义复选框弹出窗口,如果是这样,我如何才能让我为复选框列出的示例与我的asp:imageButton一起使用?

您可以使用ModalPopupXtender,而不是js confirm

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>'
ImageUrl="~/Pictures/ShoppingCart.png" />

<asp:Panel ID="pnlPopup" runat="server">
    <asp:Label ID="lblSure" Text="Are you sure?" />
    <asp:Button ID="btnYes" runat="server" Text="YES" OnClick="AddToCartBtn_Click" />
    <asp:Button ID="btnNo" runat="server" Text="NO" />
</asp:Panel>

<ajaxToolKit:ModalPopupExtender ID="mpe" runat="server"     
    TargetControlID="AddToCartBtn" 
    PopupControlID="pnlPopup" 
    OkControlID="btnYes"
    CancelControlID="btnNo" />
只需将OnClientClick替换为对函数的调用:

<asp:ImageButton ID="AddToCartBtn" runat="server" RowIndex='<%# Container.DisplayIndex %>' ImageUrl="~/Pictures/ShoppingCart.png" OnClick="AddToCartBtn_Click" OnClientClick="return Redirect();" />
function Redirect() {
    var result = confirm('Are you sure?');

    // Do something with result.

    return true; // return true no matter what the user clicked.
}

谢谢,我试图通过OnClientClick=Redirect调用它,但它没有为我做任何事情。从上面的代码$'AddToCartBtn'。单击函数如何调用此函数?它的名称是什么,或者更像是一个事件?在代码中,您附加了函数来单击对象AddToCartBtn的事件。如果你想在什么地方叫它,就把它拿出来,给它起个名字,然后叫它。