我一直在尝试从代码后面调用 .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)
我在本地检查过,工作正常。
关于javascript - 从代码隐藏调用 .js 文件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128001/