Microsoft JScript 运行时错误:需要对象
老实说,我已经在谷歌上尝试了几种不同的途径来解决这个错误,但我的头撞到了砖墙上。
我的代码中有一点jquery:
tbxProdAC.Attributes.Add("onclick", "$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');");
但是,每当我单击文本框并打开调试时,我都会在以下行收到上述错误:
<input name="ctl00$ContentPlaceHolder1$tbxProdAC" type="text" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$tbxProdAC\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="ctl00_ContentPlaceHolder1_tbxProdAC" class="completionList2" onclick="$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');" style="z-index: 1; left: 200px; top: 475px; position: absolute; height: 20px; width: 345px;" />
现在 ctl00$ContentPlaceHolder1$tbxProdAC 并不真正“存在”。这是内容页面上内容占位符内的控件。
如何确保 jquery 与正确的 ID 一致,或者如何确保内容占位符不会修改原始控件 ID?
任何帮助都得到了很大的帮助,因为我正在疯狂地尝试不同的事情!
I have honestly tried several different avenues from google on this erro but I am hitting my head against a brick wall.
I have this bit of jquery in my code behind:
tbxProdAC.Attributes.Add("onclick", "$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');");
However, whenever I click on the textbox, with debugging switched on, I received the above error on the following line:
<input name="ctl00$ContentPlaceHolder1$tbxProdAC" type="text" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$tbxProdAC\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="ctl00_ContentPlaceHolder1_tbxProdAC" class="completionList2" onclick="$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');" style="z-index: 1; left: 200px; top: 475px; position: absolute; height: 20px; width: 345px;" />
Now ctl00$ContentPlaceHolder1$tbxProdAC doesn't really 'exist. This is a control that is on a content page within a content place holder.
How do I go about ensuring that the jquery reconciles with the correct ID or how do I ensure that the content place holder does not amend the original control ID?
Any help greatly received as I am going mad trying different things!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 onchange 事件处理程序中,您不需要 javascript 位,您可以直接调用该函数,您只需要通过 href 调用该函数即可。您也无法按照超时期间的方式传递参数。
可能值得在函数中调用超时,而不是在调用事件时调用超时。
无论哪种方式,您都可以在超时时异常调用该函数,例如;
另外,我会将其他事件单击等中发生的所有事情移至它们自己的函数中。如果你将所有这些东西移到函数中,或者移到外部 js 文件中,你将能够跟踪更多正在发生的事情,因为它会更清晰。如果您还没有使用 firebug 进行调试,也可以使用它。如果您需要从页面中获取生成的变量,那么只需获取页面中的变量并从 js 中调用它们即可。
jquery 中可能会过于具体并遍历 dom 来查找内容而不是显式引用它,这不仅使代码更容易,而且使其更通用,因此更可重用。
In the onchange event handler you don't need the javascript bit you can call the function directly you only need to do this is you a calling the function by an href. You also cannot pass parameters the way you are doing in the timeout.
Might be worth calling the timeout in the function rather than as you call the event.
Either way you can call the function in the timeout anomously eg;
Also I would move all the things that are happening in the other events click etc into their own functions. If you move all this stuff into functions perhaps into an external js file you will be able to trace more what is going on as it will be clearer. Also use firebug for debugging if you are not already. If you need to get generated vars from the page then just about the vars in the page and call them from the js.
It is possible in jquery do to be too specific and traverse the dom to find stuff rather than refer to it explicitly not only does it make the code easier it makes it more generic therefore more reusable.
您需要获取客户端 ID,这必须在报价之外完成。
"$('radProdAC.ClientID').attr('checked', true);
需要更改为"$('" + radProdAC.ClientID + "').attr( 'checked', true);
无论在何处键入。You need to get the client id, which has to be done outside of quotes.
"$('radProdAC.ClientID').attr('checked', true);
needs to be changed to"$('" + radProdAC.ClientID + "').attr('checked', true);
everywhere it is typed.