简体   繁体   English

一个按钮中的两个 onClick 功能,并希望一个接一个地发生一个点击功能

[英]Two onClick function in one button and want to occur one click function after another

Actually i am trying to add a dialog box on the click of a button.实际上,我试图通过单击按钮添加一个对话框。 But on the OnClick function of that button, i had already called function of a controller.但是在那个按钮的 OnClick 函数上,我已经调用了一个控制器的函数。 The HTML code for this is:用于此的 HTML 代码是:

<div class="pro-btn btn">
                        @{
<input type="button" id="place-bid-button-@Model.ProductId" class="button-1"value="@T("Nopaholics.Auction.AuctionDetailView.PlaceBid")" data-   productid="@Model.ProductId" 
onclick="Auction.placeBid('@Url.RouteUrl("Nopaholics.Auction.PlaceBid", new { productId = Model.ProductId, auctionId = Model.AuctionId })', '#product-details-form');return false;" />
}
</div>   

Now after writing below jquery, both click funtions are working simultaneously.现在在 jquery 下面写完之后,两个点击功能同时工作。 But i want to first confirm by clicking OK button of dialog box and the controller action to be called.但我想首先通过单击对话框的确定​​按钮和要调用的控制器动作来确认。

<script> 
$('#place-bid-button-'+@Model.ProductId.ToString()).click(function(e){

$( "#dialog-confirm" ).dialog({
    resizable: false,
    height:160,
    modal: true,
    buttons: {
        "OK": function() {
            $( this ).dialog( "close" );
            $("#place-bid-button-@Model.ProductId.ToString()").click();
            },
            Cancel: function(e) {
                $( this ).dialog( "close" );
                $('#place-bid-button-'+@Model.ProductId.ToString()).die();
                console.log(e);
                //return false;
                //e.preventDefault();
            }
        }
    });
});
</script>

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance..提前致谢..

You don't need multiple function.您不需要多种功能。 I would recommend too get rid of inline click handler completely.我也建议完全摆脱内联点击处理程序。

You can store the url using data-* prefixed custom attribute which can be fetched using HTMLElement.dataset property.您可以使用data-*前缀的自定义属性存储 url,该属性可以使用HTMLElement.dataset属性获取。

HTML HTML

@{
    <input type="button" class="place-bid-button button-1"
            value="@T("Nopaholics.Auction.AuctionDetailView.PlaceBid")" 
            data-url="@Url.RouteUrl("Nopaholics.Auction.PlaceBid", new { productId = Model.ProductId, auctionId = Model.AuctionId })"
            data-form="#product-details-form"/>
}

Then you can use following script然后你可以使用以下脚本

//Bind click hndler using a common class
$('.place-bid-button').click(function(e) {
    //Store the refrence in a variabled
    var self = this;

    $("#dialog-confirm").dialog({
        resizable: false,
        height: 160,
        modal: true,
        buttons: {
            "OK": function() {
                //Cal the method with url and form name
                Auction.placeBid(self.dataset.url, self.dataset.form);
                $(this).dialog("close");

            },
            Cancel: function(e) {
                $(this).dialog("close");
            }
        }
    });
});

Write both the function on single onclick attribute like: onclick="firstFunction();SecondFunction();"在单个onclick属性上编写两个函数,例如: onclick="firstFunction();SecondFunction();"

If you have confirm/alert box in firstFunction then.The confirm/alert function halts execution of the code until it's dismissed.This mean secondFunction will be called when firstFunction confirm is closed.如果您在 firstFunction 中有confirm/alert框,则confirm/alert功能会暂停代码的执行,直到它被解除。这意味着当 firstFunction 确认关闭时,将调用 secondFunction。

您想在单击按钮时执行某些操作,仅当用户单击确认对话框时才确定,因此您需要在单击“确定”按钮时调用“您的操作方法”/操作代码(您需要执行)对话框而不是再次显式调用“click()”操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM