jQuery 表单插件 & jQuery DatePicker 不能一起工作
我正在尝试使用 jQuery 的表单插件。我在此处使用 JSON 示例。
由于某种原因,当我使用 jQuery 表单插件时,日期选择器没有显示在页面上。在我的 jquery 文件中,我有:
jQuery.noConflict();
jQuery(document).ready(function($) {
$('#csf_map_form').ajaxForm(function() {
dataType: 'json',
success: process_json
});
function process_json(data) {
alert(data.csf_map_offense_group1);
}
$('#csf_map_start_date').datepicker({
dateFormat : 'mm/dd/yy',
yearRange : '2011:2011',
changeMonth: true,
changeYear: true,
defaultDate : new Date(2011, 8-1,1),
minDate : new Date(2011, 1-1,1),
maxDate : new Date(2011, 8-1, 25)
});
$('#csf_map_end_date').datepicker({
dateFormat : 'mm/dd/yy',
yearRange : '2011:2011',
changeMonth: true,
changeYear: true,
defaultDate : new Date(2011, 8-1, 25),
minDate : new Date(2011, 1-1,1),
maxDate : new Date(2011, 8-1, 25)
});
});
在我的 php 文件中,表单如下所示:
//start form
$output .= '<form id="csf_map_form" action="path-to-file/csf_map_form_handler.php" method="post" >';
//1st datepicker
$output .= '<div>';
$output .= '<label for="csf_map_start_date">Start Date:</label>';
$output .= '<div id="csf_map_start_date" style="font-size: 10px;"></div>';
$output .= '</div>';
//2nd datepicker
$output .= '<div>';
$output .= '<label for="csf_map_end_date">End Date:</label>';
$output .= '<div id="csf_map_end_date" style="font-size: 10px;"></div>';
$output .= '</div>';
//radio button group div
$output .= '<div style="float: left; width: 150px; margin: auto; padding-left: 20px; ">';
$output .= '<label for="csf_map_group1">Select Offense:</label><br />';
$output .= '<input type="radio" name="csf_map_group1" checked="checked" value="TINY">Tiny</input><br />';
$output .= '<input type="radio" name="csf_map_group1" value="MEDIUM"/>Medium</input><br />';
$output .= '<input type="radio" name="csf_map_group1" value="LARGE">Large</input><br />';
$output .= '</div>'; //end radiobuttons
$output .='<input type="submit" value="Submit" />';
$output .= '</form>';
由于某种原因,ajaxForm 与日期选择器冲突。如果我注释掉以 $('csf_map_form').ajaxForm 开头的函数和 process_json 函数,则表单中的日期选择器可以正常工作。如果我不注释掉它们,那么日期选择器就不会出现。但是,单击“提交”时会触发警报。它会提醒您在单选按钮中选择了什么。
对于发生的事情有什么想法吗?我怎样才能让他们打得好呢?我犯了什么错误?
谢谢。
I'm trying to use jQuery's Form Plugin. I'm using the JSON example here.
For some reason the datepickers are not showing on the page, when I use the jQuery Form Plugin. In my jquery file I've got:
jQuery.noConflict();
jQuery(document).ready(function($) {
$('#csf_map_form').ajaxForm(function() {
dataType: 'json',
success: process_json
});
function process_json(data) {
alert(data.csf_map_offense_group1);
}
$('#csf_map_start_date').datepicker({
dateFormat : 'mm/dd/yy',
yearRange : '2011:2011',
changeMonth: true,
changeYear: true,
defaultDate : new Date(2011, 8-1,1),
minDate : new Date(2011, 1-1,1),
maxDate : new Date(2011, 8-1, 25)
});
$('#csf_map_end_date').datepicker({
dateFormat : 'mm/dd/yy',
yearRange : '2011:2011',
changeMonth: true,
changeYear: true,
defaultDate : new Date(2011, 8-1, 25),
minDate : new Date(2011, 1-1,1),
maxDate : new Date(2011, 8-1, 25)
});
});
In my php file, the form looks like:
//start form
$output .= '<form id="csf_map_form" action="path-to-file/csf_map_form_handler.php" method="post" >';
//1st datepicker
$output .= '<div>';
$output .= '<label for="csf_map_start_date">Start Date:</label>';
$output .= '<div id="csf_map_start_date" style="font-size: 10px;"></div>';
$output .= '</div>';
//2nd datepicker
$output .= '<div>';
$output .= '<label for="csf_map_end_date">End Date:</label>';
$output .= '<div id="csf_map_end_date" style="font-size: 10px;"></div>';
$output .= '</div>';
//radio button group div
$output .= '<div style="float: left; width: 150px; margin: auto; padding-left: 20px; ">';
$output .= '<label for="csf_map_group1">Select Offense:</label><br />';
$output .= '<input type="radio" name="csf_map_group1" checked="checked" value="TINY">Tiny</input><br />';
$output .= '<input type="radio" name="csf_map_group1" value="MEDIUM"/>Medium</input><br />';
$output .= '<input type="radio" name="csf_map_group1" value="LARGE">Large</input><br />';
$output .= '</div>'; //end radiobuttons
$output .='<input type="submit" value="Submit" />';
$output .= '</form>';
For some reason the ajaxForm is conflicting with the datepicker. If I comment out the function starting $('csf_map_form').ajaxForm and the process_json function, then the datepickers in the form work fine. If I don't comment them out then the datepickers don't appear. But, the alert fires when submit is clicked. It alerts what was selected in the radiobutton.
Any ideas as to what is going on? How do I get them to play nice? What error am I making?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈喽,我想通了。
1)我将内联日期选择器更改为默认值 - 将日期选择器上的 div 替换为 .
2)我缺少$('#csf_map_form')中的'#'。
3)在csf_map_form_handler.php中,我只返回了表单信息的一部分。当我更改它以返回开始日期和结束日期以及单选按钮的选择时,它起作用了。
谢谢。
Hurray, I figured it out.
1) I changed the inline datepickers to the default-- replace the divs on the datepickers with .
2) I was missing the '#' in the $('#csf_map_form').
3) In the csf_map_form_handler.php, I was only returning only one part of the form's info. When I changed it, so that I returned the start and end dates and the radiobutton's selection, it worked.
Thank you.