c# - 确认框javascript

标签 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?');
                        }
                    }

最佳答案

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

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/

相关文章:

c# - 初学者如何在 ASP C# 网页中维护状态

javascript - Windows.event 未定义 - Firefox 中的 Javascript 错误

asp.net - 我的 asp.net dll 或页面是否会受到我电脑中的病毒的影响

c# - 如何使用 CodedUI 自动化知道光标在 WpfEdit 字段中的位置?

C#:尝试在不同类中使用控制元素 - "object reference is required"

javascript - 明确定义后图像不会改变

javascript - 使用 TinyMCE 组合内容 css 主体类

c# - Entity Framework : Error "Cannot bind argument to parameter ' Path' "on "database-update"

c# - 转义 string.format 的大括号

javascript - 将 Ruby SHA 转换为 javascript