动态加载的表单和日期选择器未初始化
情况:
我正在使用 jQuery 根据单选按钮选择加载不同的表单(存储在单独的 php 文件中)。在单独的 php 文件中,我正在初始化验证器和日期选择器。验证似乎工作正常,但日期选择器未初始化。
问题:
所有标记都是按顺序排列的,我已经将其作为一个文件的一部分进行了测试,并且工作正常,这就是为什么我从一个单独的文件加载表单的原因,我得到了问题...并且仅适用于日期选择器,而不适用于验证(它们在同一函数中初始化)。
此外:
当我尝试使用 firebug 来调试问题时,脚本标记和 jQuery 函数不会显示在 Dom 中,但我知道它正在工作,因为验证正在触发。
加载外部内容的函数:
<script type="text/javascript">
$('#hpbooking_select input[type=radio]').live('change', function() { //Setting up the chenge event for the radio button
var AffectedRegistration = $(this).parents('#hpbooking_select').parent();
//alert($(AffectedRegistration).attr('class'));
var ID = $(this).attr('id'); //getting the ID of the radio button
var IDArray = ID.split('_'); //seperating the ID into an array
var regURL = '/wp-content/themes/1000ik/hpbooking/' + IDArray[1] + '.php'; //building a url to load
var childPIPR = $(AffectedRegistration).children('.hpbooking_registration');
//alert(regURL); FOR TESTING ONLY
//$(childPIPR).html(regURL); FOR TESTING ONLY
$(childPIPR).load(regURL); //loading the url
});
</script>
<div id="hpbooking_container">
<div id="hpbooking_select">
<h2>What type of adventure are you looking for?</h2>
<fieldset>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_guidedtrip" /><label for="bookingselect_guidedtrip">A Guided Trip</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_course" /><label for="bookingselect_course">A Course</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_group" /><label for="bookingselect_group">Tour Group</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_paddlefest" /><label for="bookingselect_paddlefest">Paddlefest 2011</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_paddleplay" /><label for="bookingselect_paddleplay">Paddle and Play</label>
</fieldset>
</div>
加载的外部代码:
<script type="text/javascript">
jQuery(function(){
jQuery("#hpbookingform").validate();
jQuery('.date-pick').datePicker();
});
</script>
<form id="hpbookingform" method="post" action="/wp-content/themes/1000ik/engine-hpguidedtrips.php" >
<fieldset><LABEL for="daytrip_date">Date </LABEL><input type="text" value="" class="date-pick dp-applied required" id="daytrip_date" name="daytrip_date" /></fieldset>
</form>
资源: 日期选择器插件: http://www.kelvinluck.com/assets/ jquery/datePicker/v2/demo/index.html
The Situation:
I am using jQuery to load different forms(stored in separate php files) based on a radio button selection. Within the separate php files I am initializing a validator and datepicker. The validation seems to work fine, but the datepicker does not initialize.
The Problem:
All of the mark-up is in order, and I have tested it as part of one file and it works fine, it's only why I load the forms from a separate file that I get the problem... and only with the datepicker, not the validation(they initialize in the same function).
Furthermore:
When I try to use firebug to debug the problem, the script tags and jQuery function does not show up in the Dom, but I know it is working because the validation is firing.
The function that loads the external content:
<script type="text/javascript">
$('#hpbooking_select input[type=radio]').live('change', function() { //Setting up the chenge event for the radio button
var AffectedRegistration = $(this).parents('#hpbooking_select').parent();
//alert($(AffectedRegistration).attr('class'));
var ID = $(this).attr('id'); //getting the ID of the radio button
var IDArray = ID.split('_'); //seperating the ID into an array
var regURL = '/wp-content/themes/1000ik/hpbooking/' + IDArray[1] + '.php'; //building a url to load
var childPIPR = $(AffectedRegistration).children('.hpbooking_registration');
//alert(regURL); FOR TESTING ONLY
//$(childPIPR).html(regURL); FOR TESTING ONLY
$(childPIPR).load(regURL); //loading the url
});
</script>
<div id="hpbooking_container">
<div id="hpbooking_select">
<h2>What type of adventure are you looking for?</h2>
<fieldset>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_guidedtrip" /><label for="bookingselect_guidedtrip">A Guided Trip</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_course" /><label for="bookingselect_course">A Course</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_group" /><label for="bookingselect_group">Tour Group</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_paddlefest" /><label for="bookingselect_paddlefest">Paddlefest 2011</label>
<input type="radio" class="bookingselect" name="bookingselect" id="bookingselect_paddleplay" /><label for="bookingselect_paddleplay">Paddle and Play</label>
</fieldset>
</div>
The External Code loaded:
<script type="text/javascript">
jQuery(function(){
jQuery("#hpbookingform").validate();
jQuery('.date-pick').datePicker();
});
</script>
<form id="hpbookingform" method="post" action="/wp-content/themes/1000ik/engine-hpguidedtrips.php" >
<fieldset><LABEL for="daytrip_date">Date </LABEL><input type="text" value="" class="date-pick dp-applied required" id="daytrip_date" name="daytrip_date" /></fieldset>
</form>
Resources:
datepicker plugin: http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/index.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您会在 document.ready 块中运行这些 jquery 命令,该块将在页面完全加载后触发。
我会将脚本标签移动到页面底部。它应该在读入 html 元素后执行。
Usually you would run those jquery commands in a document.ready block which would fire after the page had been fully loaded.
I would move the script tag to the bottom of the page. It should get executed after the html elements have been read in.