开发者

JQuery Plugin - Passing options as object

开发者 https://www.devze.com 2023-04-07 19:33 出处:网络
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

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;
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号