简体   繁体   English

从代码后面调用.js文件功能

[英]Calling .js file function from code behind

I have been trying to call .js file function from code behind but function is not being called. 我一直在尝试从后面的代码中调用.js文件函数,但是未调用该函数。

I have this following html button which needs to be visible from the code behind. 我有这个下面的html按钮,需要从后面的代码中看到它。

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

I have tried following three methods so far and none of them is working. 到目前为止,我已经尝试了以下三种方法,但没有一种起作用。

 -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();")

Here is .js file function 这是.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();
    }
 }

Here is the pahe html 这是帕赫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>

try register script, like this: 尝试注册脚本,如下所示:

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

I checked this locally, worked fine. 我在本地检查过,效果很好。

jQuery method show works over display css property. jQuery方法showdisplay css属性上起作用。 It is equivalent to call .css( "display", "block") . 等效于调用.css( "display", "block")

Since you are using visibility property, method show has no effect on changing this so it remains hidden. 由于您使用的是visibility属性,因此方法show对此更改无效,因此它保持隐藏状态。 I see 2 options to fix this: 我看到2个解决方案:

  • use .css( "visibility", "visible") instead of .show() 使用.css( "visibility", "visible")代替.show()

  • replace style="visibility: hidden;" 替换style="visibility: hidden;" by style="display: none" on your input markup 通过输入标记上的style="display: none"

You just need to invoce the mentioned .js file in html before you call the script from which you are calling the function. 在调用要从中调用函数的脚本之前,只需调用html中提到的.js文件。 If you need more information, please include your HTML code. 如果您需要更多信息,请包括您的HTML代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM