当我在OnClientClick中调用它时,为什么我的Javascript函数没有被调用

时间:2014-10-03 07:40:50

标签: javascript

我今天刚学会了如何从Javascript调用后端函数,我的第一个Javascript函数工作正常。但是,当我尝试使用相同的功能并在同一个项目的不同前端网页上调用同一个按钮(即删除)时,不再调用Javascript函数。这太奇怪了。这是Javascript函数:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type = "text/javascript" >
    function watchdelete() {
        if (drpManufacturer.SelectedItem.Text != "")   // If there is a selected Manufacturer
        {
            return confirm("Are you Sure You want to delete this Dimension Detail?");
        }
        else
        {
            alert("Please select a Manufacturer first before deleting");
            return false;
        }
    }
</script>
<asp:ScriptManager ID="scripman1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

以下是对删除按钮的OnClientClick调用:

<asp:Button ID="btnDimensionDel" runat="server" Text="Delete" OnClientClick = " return watchdelete();" OnClick="btnDimensionDel_Click" />

我做错了吗?我所做的就是为以前在另一个页面上工作的Javascript函数添加几行。我觉得这个Javascript函数有一些语法错误,但你不得不原谅我,因为我是Javascript的新手。可以在Javascript中使用布尔类型作为返回值吗?非常感谢你提前。

2 个答案:

答案 0 :(得分:1)

使用此代码:

<script type = "text/javascript" >
    function watchdelete() {
        var manufacturer = document.getElementById('<%:drpManufacturer.ClientID%>');
        var strManufacturer = manufacturer.options[manufacturer.selectedIndex].value;
        if (strManufacturer)   // If there is a selected Manufacturer
        {
            return confirm("Are you Sure You want to delete this Dimension Detail?");
        }
        else {
            alert("Please select a Manufacturer first before deleting");
            return false;
        }
    }
</script>

答案 1 :(得分:0)

删除return声明:

<asp:Button ID="btnDimensionDel" runat="server" Text="Delete"
    OnClientClick="watchdelete()" OnClick="btnDimensionDel_Click" />