简体   繁体   English

为什么javascript行必须在函数中才能工作?

[英]Why does the javascript line have to be in a function to work?

If I put this javascript on my aspx page, the panel shows as expected upon button click: 如果我将此JavaScript放在aspx页面上,则单击按钮后面板将按预期显示:

 <script type="text/javascript">
 function ShowPopUp() {
     $('#<%= upNewClient.ClientID %>').show();
 }
 </script>

 <asp:Button ID="btnNewClient" runat="server" OnClientClick="javascript: ShowPopUp(); return false;" />

But if I try to do this w/o the function, it doesn't work: 但是,如果我尝试不使用该功能执行此操作,则它将不起作用:

<asp:Button ID="btnNewClient" runat="server" OnClientClick="javascript: $'(%<= upNewClient.ClientID %>'.show(); return false;"/>

The page flashes and there might be an error appearing in my javascript console, but it comes and goes too quickly for me to read it. 该页面闪烁,并且我的JavaScript控制台中可能出现错误,但是它来得太快,我无法阅读。

So, why doesn't the exact same line of code work outside of the function? 那么,为什么完全相同的代码行无法在函数之外工作?

EDIT: I've rewritten this code several times (I'm actually trying to do it in the code behind), and I mistyped it the time just before my initial posting. 编辑:我已经多次重写了此代码(实际上我正在尝试在后面的代码中执行此操作),并且在初次发布之前,我打错了时间。 The actual button tag is: 实际的按钮标签是:

<asp:Button ID="btnNewClient" runat="server" Text="Add New Client" OnClientClick="javascript: $('#<%= upNewClient.ClientID %>').show(); return false;" />

This flashes an error in my javascript console very quickly that I believe says: Uncaught Error: Syntax error, unrecognized expression: #<%= upNewClient.ClientID %> 这会很快在我的JavaScript控制台中闪烁一个错误,我相信会说:未捕获的错误:语法错误,无法识别的表达式:#<%= upNewClient.ClientID%>

You are missing the # id selector for your inline and incorrectly placed quotes.. missing closing paren 你缺少# id选择您的内联和放错了位置的报价..缺少一个右括号

so 所以

$'(%<= upNewClient.ClientID %>'.show();

should be 应该

$('#<%= upNewClient.ClientID %>').show();

You have a syntax error. 您有语法错误。 $'(%<= should be $('#<%= and you forgot the closing parenthesis for $(...) $'(%<=应该是$('#<%=并且您忘记了$(...)的右括号

The <%= ... %> syntax isn't supported within the attributes of a server-side control, so the whole thing gets sent to the browser: 服务器端控件的属性不支持<%= ... %>语法,因此整个内容都将发送到浏览器:

<input type="submit" name="btnNewClient" value="Add New Client"
    onclick="javascript: $('#&lt;%= upNewClient.ClientID %>').show(); return false;"
    id="btnNewClient" />

Because jQuery can't parse the expression, it throws an exception. 由于jQuery无法解析表达式,因此会引发异常。

Instead, you can set the button's OnClientClick property from the code-behind: 相反,您可以从后面的代码中设置按钮的OnClientClick属性:

<asp:Button ID="btnNewClient" runat="server" Text="Add New Client" />
protected override void Render(HtmlTextWriter writer)
{
    this.btnNewClient.OnClientClick =
        "$('#" + this.upNewClient.ClientID + "').show(); return false;";
    base.Render(writer);
}

(Setting the property in Render (instead of, say, OnInit , OnLoad , or OnPreRender ) prevents the value from unnecessarily bloating view state.) (在Render设置属性(而不是OnInitOnLoadOnPreRender )可以防止该值不必要地膨胀视图状态。)

我认为问题出在语法上,尝试像这样更改它

<asp:Button ID="btnNewClient" runat="server" OnClientClick="$('#<%= upNewClient.ClientID %>').show(); return false;"/>

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

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