从代码隐藏调用.js文件函数

Calling .js file function from code behind

本文关键字:文件 函数 js 调用 代码 隐藏      更新时间:2023-09-26

我一直在尝试从代码隐藏调用.js文件函数,但没有调用函数。

我有以下 html 按钮,需要从后面的代码中可见。

   <input id="btnShowMap" type="button" value="Results On Map" onclick = "ShowMap();" style="visibility: hidden;"/>

到目前为止,我已经尝试了以下三种方法,但没有一种有效。

 -ClientScript.RegisterStartupScript(Me.GetType(), "VoteJsFunc", "test();")
 -Page.ClientScript.RegisterStartupScript(Me.[GetType](), "VoteJsFunc", "alert('Sorry.You are not legible to vote')", True)
 -ClientScript.RegisterStartupScript(Me.GetType(), "VoteJsFunc", "test();")

这是.js文件功能

function test() {
var hdLat = $('input[id$=hdVendorLat]').val();
    var hdLng = $('input[id$=hdVendorLng]').val();
    if (hdLat != 0 && hdLng != 0) {
        $('#btnShowMap').show();
    }
    else {
        $('#btnShowMap').hide();
    }
 }

这是 pahe html

 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="updSearch" UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearch" />
    </Triggers>
    <ContentTemplate>
        <asp:HiddenField ID="hdVendorLat" runat="server" Value="0" />
        <asp:HiddenField ID="hdVendorLng" runat="server" Value="0" />
        <asp:HiddenField ID="hdVenID" runat="server" Value="" />
  <asp:Panel ID="pnlExport" runat="server" Enabled="true">      
  <asp:Button ID="btnSearch" runat="server" Text="Search" Width="90px" />                          
  <input id="btnShowMap" type="button" value="Results On Map" onclick   = "ShowMap();" style="visibility: hidden;"  />
</asp:Panel>
<script type="text/javascript" src="/scripts/inspector-search.js"></script>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

尝试注册脚本,如下所示:

Page.ClientScript.RegisterStartupScript(GetType(), "VoteJsFunc", "test()", True)

我在本地检查了一下,工作正常。

jQuery 方法show适用于display css 属性。相当于调用.css( "display", "block")

由于您使用的是visibility属性,因此方法 show 对更改此属性没有影响,因此它保持隐藏状态。我看到 2 个选项可以解决此问题:

  • 使用.css( "visibility", "visible")而不是.show()

  • style="visibility: hidden;"替换为输入标记上的style="display: none"

调用调用函数的脚本之前,您只需要在 html 中调用上述.js文件。如果您需要更多信息,请提供您的 HTML 代码。