jQuery 验证插件和 Django 管理的困难
我正在使用 Django 1.2.4 管理界面、jQuery 1.4.4 和 jQuery 验证插件。我看到一些奇怪的问题。
jQuery 验证插件表示它适用于 jQuery 1.4.2。我使用 1.4.4 会导致问题吗?我还使用 jQuery UI,它需要 1.4.4。我怎样才能同时使用它们?
以下是我的管理页面中包含的脚本:
# 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 管理命名空间 jQuery 到 django.jQuery,以避免破坏使用 $
符号的其他脚本。 (Django admin 使用 jQuery 1.4.2,它也包含在页面中。)在我的 foo-slider.js 脚本的顶部,我有这样一行:
var $ = django.jQuery;
如果我包含 jQuery 验证插件脚本,我在 Firebug 控制台上得到以下内容:
>>> $
function()
然后,我将以下代码行添加到我的自定义 JS 文件中:
$("form").validate();
然后,我在 Firebug 控制台中得到以下内容:
>>> $
anonymous()
这令人沮丧,可能与以后的问题有关。
我添加此代码:
$("form").validate();
$(".required").nextAll('input').rules('add', {
required: true
});
但没有进行验证。
标记如下所示:
<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>
然后,我在方法中添加以下代码:
$(sprintf("#%s, #%s, #%s", id_foo, id_bar, id_baz)).rules("add",
{
range: [slider.slider('option', 'min'), slider.slider('option', 'max')],
});
(sprintf 执行其听起来的操作;id_*
参数是 元素的 id。 ) 现在进行验证。到底是怎么回事?我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许 noconflict 页面会有一些帮助?而不是
尝试
perhaps the noconflict page could be of some help? instead of
try