【发布时间】:2013-10-14 15:31:53
【问题描述】:
我想将此简码生成表单转换为一个将根据表单中选择的值生成两种不同简码之一的表单。所以,一个单选按钮说,“你想构建哪个简码?”然后他们选择它,然后他们继续使用其他字段来填写属性值。然后,当需要生成代码时,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]';
}
--- 更新 ---
找到了一种更经济的方式,无需重复期权指数。请参阅下面的答案。
【问题讨论】:
-
请参阅stackoverflow.com/questions/18043452/… 了解如何获取单选按钮的值。然后将其用作短代码数组的索引。
-
谢谢,巴马尔。我实际上得到了它的工作(我使用下拉而不是收音机,但只是为了美观)。我将用它更新帖子,因为我想看看是否有更经济的方法来做到这一点。但它有效!
标签: javascript jquery conditional-statements shortcode