确认框javascript

时间:2011-11-17 16:38:57

标签: c# javascript asp.net

问题:

每当我点击保存按钮时,我都会弹出一个确认框。单击“确定”时,它可以保存数据。但是当我点击取消时它不会取消更改。它会保存它们。你能帮我解决一下这个问题:

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

1 个答案:

答案 0 :(得分:4)

您需要在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" />