Difficulty with jQuery Validation plugin and Django Admin
I'm using the Django 1.2.4 admin interface, jQuery 1.4.4, and the jQuery Validation plugin. I'm seeing some weird issues.
The jQuery validation plugin says that it works with jQuery 1.4.2. Could it be causing problems that I'm using 1.4.4? I'm also using jQuery UI, which requires 1.4.4. How can I use them both?
Here are the scripts that are included on my admin page:
# this will cause both jQuery 1.4.2 and 1.4.4 to be included. Not sure if that's a problem or not.
# (jQ 1.4.2 is in the admin by default, but jQ-ui wants 1.4.4.)
js = ("js/foo-slider.js",
"js/lib/jquery-1.4.4.min.js",
"js/lib/jquery.validate.pack.js",
"js/lib/sprintf.js",
"js/lib/jquery-ui-slider/jquery.ui.core.js",
"js/lib/jquery-ui-slider/jquery.ui.widget.js",
"js/lib/jquery-ui-slider/jquery.ui.mouse.js",
"js/lib/jquery-ui-slider/jquery.ui.slider.js",
)
Django Admin namespaces jQuery to django.jQuery to avoid breaking other scripts that use the $
symbol. (Django admin uses jQuery 1.4.2, which is also included in the page.) At the top of my foo-slider.js
script, I have the line:
var $ = django.jQuery;
If I include the jQuery validation plugin script, I get the following on the Firebug console:
>>> $
function()
I then add the following line of code to my custom JS file:
$("form").validate();
Then, I get this in the Firebug console:
>>> $
anonymous()
This is frustrating, and perhaps related to later issues.
I add this code:
$("form").validate();
$(".required").nextAll('input').rules('add', {
required: true
});
but no validation happens.
The markup looks like:
<form id="foo_form" method="post" action="" enctype="multipart/form-data"><div style="display: none;">
<!-- ... -->
<fieldset class="module aligned ">
<div class="form-row foo">
<div>
<label class="required" for="id_foo">Foo:</label>
<input type="text" name="foo" value="80" class="vIntegerField valid" id="id_foo">
<p class="help">help text</p>
</div>
</div>
<!-- ... --->
</form>
I then add the following code in a method:
$(sprintf("#%s, #%s, #%s", id_foo, id_bar, id_baz)).rules("add",
{
range: [slider.slider('option', 'min'), slider.slider('option', 'max')],
});
(sprintf does what it sounds like; the 开发者_如何学运维id_*
arguments are ids of <input>
elements.) Now validation occurs. What is going on? Am I doing something wrong?
perhaps the noconflict page could be of some help? instead of
var $ = django.jQuery;
try
(function($) {
//code that expects the $ alias
})(django.jQuery);
精彩评论