$ Find返回null [英] $Find returns null
问题描述
我在页面上有以下JScript
I have the following JScript on a page
<script type="text/javascript">
function ProcessButtonDisable() {
var button = $find("<%=ProcessButton.ClientID %>");
button.disabled = true;
}
</script>
及以后
<asp:Button ID="ProcessButton" Text="Process All" runat="server" OnClick="Process_Click" OnClientClick="ProcessButtonDisable()" />
在运行页面并触发按钮时我得到
when running the page and firing off the button i get
Microsoft JScript运行时错误:无法设置属性'disabled'的值:对象为null或未定义
Microsoft JScript runtime error: Unable to set value of the property 'disabled': object is null or undefined
,动态页面已将其转换为:
and the dynamic page has converted it to:
<script type="text/javascript">
function ProcessButtonDisable() {
var button = $find("ctl00_ctl00_BodyContentPlaceHolder_MainContentPlaceHolder_ProcessButton");
button.disabled = true;
}
</script>
<input type="submit" name="ctl00$ctl00$BodyContentPlaceHolder$MainContentPlaceHolder$ProcessButton" value="Process All" onclick="ProcessButtonDisable();" id="ctl00_ctl00_BodyContentPlaceHolder_MainContentPlaceHolder_ProcessButton" />
由于控件已明确定义,并且客户端ID似乎返回了正确的ID,我不知道这是怎么回事
as the control is clearly defined and the client id seems to be returning the correct id i don't know whats wrong
有帮助吗?
ps,以防代码中不清楚,目的是防止用户单击并重新发送请求,以免页面在首次单击后有时间重新加载.
ps in case this is not clear from the code the purpose of this is to prevent he user from clicking on the and resending the request before the page has time to reload after the initial click
推荐答案
您需要使用点表示法,因为 find()
是jQuery函数,如下所示:
You need to use the dot notation, as find()
is a jQuery function, like this:
<script type="text/javascript">
function ProcessButtonDisable() {
var button = $.find("<%=ProcessButton.ClientID %>");
button.disabled = true;
}
</script>
此外,如果您要麻烦地在jQuery逻辑中查找DOM元素,则不必费心在服务器控件上连接 OnClientClick
;通过jQuery连接click事件或将元素本身传递给JavaScript函数:
Also, if you are going to take the trouble to look up the DOM element in your jQuery logic, then do not bother wiring up the OnClientClick
on the server control; either wire up the click event via jQuery or pass the element itself to the JavaScript function:
使用jQuery连接click事件(推荐):
Using jQuery to wire up the click event (recommended):
<script type="text/javascript">
$(document).ready(function() {
$("#<%=ProcessButton.ClientID%>").click(function() {
$(this).disabled = true;
});
});
</script>
使用 OnClientClick
属性连接click事件并传递元素(不推荐):
Using the OnClientClick
attribute to wire up the click event and pass the element (not recommended):
<asp:Button ID="ProcessButton" Text="Process All" runat="server" OnClick="Process_Click"
OnClientClick="ProcessButtonDisable(this)" />
<script type="text/javascript">
function ProcessButtonDisable(elem) {
elem.disabled = true;
}
</script>
这篇关于$ Find返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!