0

我想将此简码生成表单转换为一个将根据表单中选择的值生成两种不同简码中的任何一种的简码。所以,一个单选按钮说,“你想构建哪个简码?” 然后他们选择它,然后他们继续使用其他字段来填写属性值。然后,当需要生成代码时,JS 将根据单选按钮问题调整其输出。我试过自己修改它,但问题是,这个脚本从选项索引生成属性,所以我不知道如何包含一个不进入索引的选项:

var table = form.find('table');
form.appendTo('body').hide();
form.find('#myshortcodeidstem-submit').click(function(){
    var options = { 
        'shortcodename' : '', \\ THIS IS THE ONE TO DETERMINE THE SHORTCODE NAME
        'attribute' : '', \\ THIS IS THE ATTRIBUTE THAT BOTH SHORTCODES SHARE
    };
    var shortcode = '[myshortcode'; \\ THIS LINE NEEDS TO BE CONDITIONAL ON OPTION 1
        for( var index in options) {
        var value = table.find('#myshortcodeidstem-' + index).val();

        if ( value !== options[index] && value != null )
        shortcode += ' ' + index + '="' + value + '"';
        }

    shortcode += '] Content Here [/myshortcode]'; \\ THIS LINE CONDITIONAL ON OP1

- - 更新 - -

Barmar 为我指出了正确的方向,我得到了它的工作,但我想知道是否有更经济的方法来做。这是我所拥有的:

var table = form.find('table');
    form.appendTo('body').hide();
    form.find('#myshortcodeid-submit').click(function(){
        var codeselector = table.find('#myshortcodeid-codeselector').val();
        if (codeselector === '1'){          
        var options = { 
            'attribute'   : '',
            };
        var shortcode = '[shortcode_one';
            for( var index in options) {
            var value = table.find('#myshortcodeid-' + index).val();

            if ( value !== options[index] && value != null )
                shortcode += ' ' + index + '="' + value + '"';
        }
        shortcode += '] Content Here [/shortcode_one]';
        }
        if (codeselector === '2'){          
        var options = { 
            'attribute'   : '',
            };
        var shortcode = '[shortcode_two';
            for( var index in options) {
            var value = table.find('#myshortcodeid-' + index).val();

            if ( value !== options[index] && value != null )
                shortcode += ' ' + index + '="' + value + '"';
        }
        shortcode += '] Content Here [/shortcode_two]';
        }

- - 更新 - -

找到了一种更经济的方式,无需重复期权指数。请参阅下面的答案。

4

1 回答 1

1

这是我能想到的最经济的方法。不会以这种方式重复选项索引。工作很好。只需为选择简码的下拉字段创建一个 var,然后执行引用该 var 值的 if 语句。

        var codeselector = table.find('#myid-codeselector').val();
        if (codeselector === '1'){          
        var shortcode = '[shortcode_one';
        }
        if (codeselector === '2'){          
        var shortcode = '[shortcode_two';
        }
        var options = { 
            'attribute'   : '',
            };
            for( var index in options) {
            var value = table.find('#myid-' + index).val();

            if ( value !== options[index] && value != null )
                shortcode += ' ' + index + '="' + value + '"';
        }
        if (codeselector === '1'){          
        shortcode += '] Content Here [/shortcode_one]';
        }
        if (codeselector === '2'){          
        shortcode += '] Content Here [/shortcode_two]';
        }
于 2013-10-07T22:56:46.337 回答