简体   繁体   English

如何避免多次显示警报

[英]How to avoid alert displayed multiple times

The webpage has an UpdatePanel with a grid view having checkboxes on each row, and a couple of buttons of the same class "cssUpdateEM". 该网页具有一个带有网格视图的UpdatePanel,该网格视图在每行上都具有复选框,以及几个具有相同类“ cssUpdateEM”的按钮。 Outside the update panel I have another button "btnExportToExcel" having the "cssUpdateEM" class. 在更新面板之外,我还有另一个具有“ cssUpdateEM”类的按钮“ btnExportToExcel”。

The buttons within the update panel, if clicked, display the alert message (if no items selected) only once. 如果单击更新面板中的按钮,则仅显示一次警报消息(如果未选择任何项目)。 The issue is with the outer button "btnExportToExcel". 问题在于外部按钮“ btnExportToExcel”。 If the page is posted back n times, and btnExportToExcel is clicked, and no items selected, the alert message will show n times instead of only once. 如果该页面回发了n次,并且单击了btnExportToExcel,并且未选择任何项目,则警报消息将显示n次,而不是仅一次。

 //************************** // Pick the selected Items //************************** $('.cssUpdateEM').click(function () { var SelectedEMItemIDs = ""; var strDelim = ""; $('.cssItemEM').each(function () { if ($(this).is(':checked') == true) { SelectedEMItemIDs += strDelim + $(this).attr('eid'); strDelim = ";"; } }); if (SelectedEMItemIDs == "") { alert('Please select the item(s).'); return false; } else { $('#hdnSelectedItemID').attr('Value', SelectedEMItemIDs); return true; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 

Any idea why is this happening, and how to fix this? 知道为什么会这样,如何解决吗?

I solved this problem. 我解决了这个问题。 I'd like to mention that I'm using a master page. 我想提一下,我正在使用母版页。

So, code before : 因此,在代码之前

<script type="text/javascript">

    function BindControlEvents() {
    //jQuery is wrapped in BindControlEvents function so it can be re-bound after each callback.

        $(document).ready(function () {

            $('.cssUpdateEM').click(function () {
                //the original function body here
            });

           // ... other functions here

        })
    }

// ... other functions here

The script ends with: 脚本结尾为:

    Sys.Application.add_load(BindControlEvents);
</script>

The code after has the btnExportToExcel event handler moved outside of the $(document).ready. 后面代码将btnExportToExcel事件处理程序移至$(document).ready之外。

$('#btnExportToExcel').click(function () {
    //code including the alert here
}

I hope this explanation will help somebody, sometime. 我希望这种解释能在某个时候对某人有所帮助。

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

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