I have a JQuery date 开发者_StackOverflow中文版mask, but when I run the page it throws an error "Microsoft JScript runtime error: Object doesn't support this property or method".
Now, the control that this particular JQuery is meant to be working is added dynamically to a repeater control. Through this, I have looked at the ids of the control, where it was breaking and stopping in Visual Studio and what is being shown on the aspx page. The ids are identical except of the "#" that JQuery has at the start, which is not on the page.
In my JQuery code I have:
JQuery(function ($) {
$('#<%=date.ClientID %>').mask("99/99/9999");
});
Is there away to tell JQuery not to include the "#" when finding the control? I have used UniqueID but this changes any underscore into "$", which is not the same as what is on the page. My only problem is with "#" sign at the start of the ID.
Can anyone help?
Thanks
<asp:TextBox ID="date" runat="server"></asp:TextBox>
<script type="text/javascript">
alert($('#<%=date.ClientID %>').attr('id'));
</script>
This code alerts the right id, so the '#' is not the problem. Check if mask() is a valid function and if your code runs before the textbox is on the page. Try:
$(document).ready(function(){$('#<%=date.ClientID %>').mask("99/99/9999");})
Note: the <%=
ServerSideCode %>
block MUST be on the aspx page, it does not work in a .js file!
Or you can add a classname as Ives suggested, then the server side code isn't needed.
Maybe you forgot to include the mask library.
<script src="jquery.maskedinput.js" type="text/javascript"></script>
To make things easier you can add a class (class="datemasked") for the control and then use the following to make it work:
$('.datemasked').mask("99/99/9999");
});
Try to split up your code and use Firebug to debug. If you write:
var element = $('#<%=date.ClientID %>');
$(element).mask("99/99/9999");
And then step through the code, you can at least see if the element is found. You can also list all the available methods on the object to see that the mask function is available.
As Ives said, make sure that the library is included.
Edit: From what I gather, your main problem is that you're mixing C# and JavaScript. The <% date.ClientID %> only makes sense in C#, so you need to somehow send this as a parameter to the function setting up the JavaScript.
A better solution, as described by Ives is to use the CssClass on the element and then use $('.classname') in the jQuery selector. This assumes that you only have one instance of the element though, which might not be your case.
精彩评论