问题:

我有一个确认框,每当我单击保存按钮时,都会弹出该确认框。单击“确定”后,它可以很好地保存数据。但是,当我单击“取消”时,它不会取消更改。而是保存它们。您能帮我解决这个问题吗?

这是它的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?');
                        }
                    }

最佳答案

您需要在OnClientClick声明中添加return语句:

OnClientClick="return Validate_view();"


在显示警报的情况下,我将代码更改为:

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


编辑:这是一个完整的例子

<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" />

关于c# - 确认框javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8170859/

10-10 23:38