JQuery Plugin - Passing options as object
I built a custom Message Dialog Box JQuery plugin that works great. However, I am trying to allow users to set the options from input text fields (i.e. background color, font size, etc). I then create a object with all the options that are not empty, and pass to my plugin to $.extend with the default options. Cant get it to work! Any ideas?
messageBox_settings is the class for the input fields to be used as options.
The field 'id' = option name.
I am looping through each field and checking for any that are not empty.
The plugin works fine when manually defining individual options in the plugin function call.
$('button#show_messagebox').click(function(){
var optionLabel = '';
var optionValue = '';
var optionsArr = {};
$('.messageBox_settings').each(function(){
if($(this).val()!=""){
optionLabel = $(this).attr('id');
optionValue = $(this).val();
$.extend(optionsArr,{optionLabel:optionValue});
}
});
//optionsArr = {optionLabel:optionValue}; Just a test when passing one option
$('.messageBox_test').messageBox(o开发者_Go百科ptionsArr);
optionsArr is a javascript object, which can be used like an associative array indexed by the object's property name.
var options = {};
$('.messageBox_settings').each(function(){
if($(this).val()!=""){
optionLabel = $(this).attr('id');
optionValue = $(this).val();
options[optionLabel] = optionValue;
}
});
精彩评论