简体   繁体   English

确认框javascript

[英]Confirmation box javascript

Problem: 问题:

I have a confirmation box which pops up whenever I click on the save button. 我有一个确认框,每当我单击保存按钮时,都会弹出该确认框。 It saves the data fine when I click on OK. 当我单击“确定”时,它将保存数据。 But when I click on cancel it does not cancel changes. 但是,当我单击“取消”时,它不会取消更改。 It saves them instead. 而是保存它们。 Could you please help me solve this issue: 您能帮我解决这个问题吗?

Here is the JavaScript for it: 这是它的JavaScript:

/
<asp:Button ID="view_btn_save" Text="Save" ValidationGroup="view" OnClick="view_btn_save_click"
                        OnClientClick="Validate_view()" runat="server"
                        />                        
                </td>
                <script type="text/javascript">
                    function Validate_view() {
                        var value = document.getElementById('<%=view_txt_name.ClientID%>').value;
                        var value2 = document.getElementById('<%=view_txt_title.ClientID%>').value;
                        var value3 = document.getElementById('<%=view_txt_description.ClientID%>').value;
                        var value4 = document.getElementById('<%=view_txt_pixelwidth.ClientID%>').value;
                        var value5 = document.getElementById('<%=view_txt_pixelheight.ClientID%>').value;

                        if (value == '' || value2 == '' || value3 == '' || value4 == '' || value5 == '') {
                            return alert('Please enter the missing fields');
                        }                        
                        else {
                            return confirm('Confirm changes?');
                        }
                    }

You need to add a return statement to the OnClientClick declaration: 您需要在OnClientClick声明中添加return语句:

OnClientClick="return Validate_view();"

In the case where you show an alert, I would change your code to this: 在显示警报的情况下,我将代码更改为:

alert("...");
return false;

EDIT : Here's a full example 编辑 :这是一个完整的例子

<script type="text/javascript"> 
    function Validate_view() { 
        var value = document.getElementById('<%=view_txt_name.ClientID%>').value; 
        var value2 = document.getElementById('<%=view_txt_title.ClientID%>').value; 
        var value3 = document.getElementById('<%=view_txt_description.ClientID%>').value; 
        var value4 = document.getElementById('<%=view_txt_pixelwidth.ClientID%>').value; 
        var value5 = document.getElementById('<%=view_txt_pixelheight.ClientID%>').value; 

        if (value == '' || value2 == '' || value3 == '' || value4 == '' || value5 == '') { 
            alert('Please enter the missing fields'); 
            return false;
        }                         
        else { 
            return confirm('Confirm changes?'); 
        } 
    } 
</script>
<asp:Button ID="view_btn_save" runat="server" Text="Save" OnClientClick="return Validate_view()" OnClick="view_btn_save_click" />                         

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

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