【发布时间】:2016-11-14 00:13:28
【问题描述】:
我一直在一个页面上使用 2 个不同版本的 jQuery,并试图利用“noConflict”。
如果我为每个 jQuery 版本建立一个变量(例如:$a 和 $b),我会用 '$a' 和 '$b' 替换所有 $'s 吗?
例如(完整代码的 sn-p [显示在底部]).. 不确定将 $b 放在哪里。
<script>
$b(document).ready(function() {
function clone(){
var $cloned = $('table tr:last').clone();
var oldIndex = $cloned.find('input').attr('name').match(/\d+/);
var newIndex = parseInt(oldIndex,10)+1;
$cloned.find('input').each(function(){
var newName = $(this).attr('name').replace(oldIndex, newIndex);
$(this).attr('name', newName);
});
$cloned.insertAfter('table tr:last');
}
$('p.add-btn').click(clone)
;
</script>
下面是 HTML 表单代码,以及 jQuery 克隆函数以及日期选择器函数。目前,当我启用一个时,另一个功能不起作用。感谢您提供任何线索。
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>var $a = jQuery.noConflict(true); //sets up 1st version of jquery to run for Zebra datepicker</script>
<script type="text/javascript" src="zebra_datepicker/js/zebra_datepicker.js"></script>
<link rel="stylesheet" href="zebra_datepicker/css/default.css" type="text/css">
<script>
$a(document).ready(function() {
$a('#SubmissionDate').Zebra_DatePicker({
view: 'years',
readonly_element: true
});
$a('#ClassYear').Zebra_DatePicker({
view: 'years',
format: 'Y',
readonly_element: true
});
});
</script>
<script>
$b(document).ready(function() {
function clone(){
var $cloned = $('table tr:last').clone();
var oldIndex = $cloned.find('input').attr('name').match(/\d+/);
var newIndex = parseInt(oldIndex,10)+1;
$cloned.find('input').each(function(){
var newName = $(this).attr('name').replace(oldIndex, newIndex);
$(this).attr('name', newName);
});
$cloned.insertAfter('table tr:last');
}
$('p.add-btn').click(clone)
;
</script>
<table id="FormLayout" cellspacing="3" cellpadding="3">
<tr>
<td class="FormLabel" width="100px"><strong>Class Year*</strong></td>
<td width="100px"><input type="text" id="ClassYear" name="ClassYear_1" class="required" value="<%= Request.form("ClassYear") %>" tabindex="4" /></td>
</tr>
<tr><td>
<button type="button" id="add-btn">Add Class Year</button>
</td></tr>
<tr>
<td colspan="4" align="center"><input type="hidden" name="FormSubmit" value="True" />
<input type="submit" value="Submit" tabindex="8" />
</td>
</tr>
</table>
【问题讨论】:
标签: javascript jquery html forms dynamic