0
SETUP 我用選的插件(http://harvesthq.github.com/chosen/) 和使用relCopy腳本克隆選的插件選項不會傳遞到克隆行
問題 我可以成功地克隆行,但像「max_selected_options」選項不會傳遞給克隆的行。 請告訴我我做錯了什麼?
小提琴文件: http://jsfiddle.net/KjNb5/
HTML代碼
<label>Select Options</label>
<select data-placeholder="You may select upto Two options" name="Opt_1" id="Opt_1" class="chosen-select" multiple tabindex="6" style="width: 280px; ">
<option value=""></option>
<optgroup label="NFC EAST">
<option>Dallas Cowboys</option>
<option>New York Giants</option>
<option>Philadelphia Eagles</option>
<option>Washington Redskins</option>
</optgroup>
<optgroup label="NFC NORTH">
<option>Chicago Bears</option>
<option>Detroit Lions</option>
<option>Green Bay Packers</option>
<option>Minnesota Vikings</option>
</optgroup>
</select>
<p><a href="#" class="copy" rel=".addMoreFiles">Add More</a></p>
JS文件
/**
* jQuery-Plugin "relCopy"
*
* @version: 1.1.0, 25.02.2010
*
* @author: Andres Vidal
* [email protected]
* http://www.andresvidal.com
*
* Instructions: Call $(selector).relCopy(options) on an element with a jQuery type selector
* defined in the attribute "rel" tag. This defines the DOM element to copy.
* @example: $('a.copy').relCopy({limit: 5}); // <a href="example.com" class="copy" rel=".phone">Copy Phone</a>
*
* @param: string excludeSelector - A jQuery selector used to exclude an element and its children
* @param: integer limit - The number of allowed copies. Default: 0 is unlimited
* @param: string append - HTML to attach at the end of each copy. Default: remove link
* @param: string copyClass - A class to attach to each copy
* @param: boolean clearInputs - Option to clear each copies text input fields or textarea
*
*/
(function($) {
$.fn.relCopy = function(options) {
var settings = jQuery.extend({
excludeSelector: ".exclude",
emptySelector: ".empty",
copyClass: "copy",
append: '',
clearInputs: true,
limit: 0 // 0 = unlimited
}, options);
settings.limit = parseInt(settings.limit);
// loop each element
this.each(function() {
// set click action
$(this).click(function(){
var rel = $(this).attr('rel'); // rel in jquery selector format
var counter = $(rel).length;
// stop limit
if (settings.limit != 0 && counter >= settings.limit){
return false;
};
var master = $(rel+":first");
var parent = $(master).parent();
var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append);
//Remove Elements with excludeSelector
if (settings.excludeSelector){
$(clone).find(settings.excludeSelector).remove();
};
//Empty Elements with emptySelector
if (settings.emptySelector){
$(clone).find(settings.emptySelector).empty();
};
// Increment Clone IDs
if ($(clone).attr('id')){
var newid = $(clone).attr('id') + (counter +1);
$(clone).attr('id', newid);
};
// Increment Clone Children IDs
$(clone).find('[id]').each(function(){
var newid = $(this).attr('id') + (counter +1);
$(this).attr('id', newid);
});
//Clear Inputs/Textarea
if (settings.clearInputs){
$(clone).find(':input').each(function(){
var type = $(this).attr('type');
switch(type)
{
case "button":
break;
case "reset":
break;
case "submit":
break;
case "checkbox":
$(this).attr('checked', '');
break;
default:
$(this).val("");
}
if ($(this).hasClass('chosen-select')) {
$(this).next('.chosen-container').remove();
$(this).css({display: "inline-block"}).removeClass("chosen-done");
var that = $(this);
setTimeout(function(){ that.removeData('chosen').chosen();},0);
}
});
};
$(parent).find(rel+':last').after(clone);
return false;
}); // end click action
}); //end each loop
return this; // return to jQuery
};
})(jQuery);
$(function(){
var removeLink = ' <a class="remove" href="#" onclick="$(this).parent().slideUp(function(){ $(this).remove() }); return false">remove</a>';
$('a.copy').relCopy({append: removeLink});
});
$(document).ready(function(){
$(".chosen-select-deselect").chosen({allow_single_deselect:true});
$(".chosen-select").chosen({max_selected_options: 2});
$(".chosen-select").bind("chosen:maxselected", function() { alert("Maximum limit reached"); });
$(".chosen-select").trigger('chosen:activate');
});
謝謝Varinder,你的代碼很好,我有幾個問題你。 A)是否有可能在第一行避免「刪除」鏈接?因爲我不希望有人刪除所有行。 B)我們能限制最多5行克隆嗎? C)是否有可能傳遞所有這些克隆參數來上傳Form?謝謝。 – kevin
嘿凱文,確定的人 - 你可以做你想做的任何事情:) 更新的小提琴:http://jsfiddle.net/KjNb5/4/ A)是的,需要改變一下代碼以確保它插入第一個字段時刪除「刪除」鏈接。 B)是,JS line:40 C)是的,新添加的字段現在以「Opt_1」,「Opt_2」等格式獲得id和name屬性,用一個虛假的提交按鈕添加一個表單標籤,該按鈕只是提醒所選的選項。 JS line:32 希望有幫助, Cheers Varinder – Varinder