简体   繁体   English

在选择选项时动态显示表单域

[英]Show a form-field dynamically on option select

This is my fiddle : DEMO 这是我的小提琴: DEMO

I am adding the template ID read-only fields, based on selection of option. 我根据选项的选择添加模板ID只读字段。 1 for SMS, 2 for email etc.. 1个用于SMS,2个用于电子邮件等。

Since there is a provision to add new category, how to dynamically add template IDs to newly added options? 既然有添加新类别的规定,那么如何将模板ID动态添加到新添加的选项中?

//Adding Template ID based on option
$('#categoryevent').on('click', function() {
  $('.actionConfig').empty();
  var z = $("#categoryevent option:selected").text();  
  if (z == 'sms') {
    var smsConfig = '<div class=form-group><label class="col-sm-2 control-label"for=templateId>Template ID: </label><div class=col-sm-8><input class=form-control id=templateId name=templateId value="1" readonly="readonly"></div></div>';
    $('.actionConfig').append(smsConfig);
  }
});

Welcome to SO, you can do something like this, 欢迎使用SO,您可以执行以下操作,

Set value of option as Template-Id 1,2,3 now when you add new category maintain counter and add it as value of newly added option. 现在,当您添加新类别维护计数器并将其添加为新添加的选项的值时,将选项的值设置为Template-Id 1,2,3。

Now keep other things same just get template id from option value. 现在保持其他条件不变,只是从选项值中获取模板ID。

something like this, 像这样

var opval = get option value here..
     var smsConfig = '<div class=form-group><label class="col-sm-2 control-label"for=templateId>Template ID: </label><div class=col-sm-8><input class=form-control id=templateId name=templateId value="+opval+" readonly="readonly"></div></div>';
        $('.actionConfig').append(smsConfig);

Hope this helps. 希望这可以帮助。

Edit: 编辑:

answer jsfiddle 回答jsfiddle

Instead of checking for the textual representation of the option, you should check for the value attribute. 代替检查选项的文本表示,您应该检查value属性。 Since you did not add the value attributes yet for the options you hardcoded, you will have to do this as well, ie change: 由于尚未为硬编码的选项添加值属性,因此也必须执行此操作,即更改:

<option>SMS</option>

to

<option value="0">SMS</option>

Alternatively, if you don't want to use the standard value attribute (eg because you want to avoid default beheviour) you can instead go for <option data-selectionId="1">SMS</option> . 另外,如果您不想使用标准value属性(例如,因为要避免使用默认行为),则可以使用<option data-selectionId="1">SMS</option> Obviously increment the id for each option you have in your select. 显然,增加您选择的每个选项的ID。

Now if you create a new option, you will have to set this data: 现在,如果您创建一个新选项,则必须设置以下数据:

  var val = $("#new-option-event").val().trim();
  var opt = '<option value="'+ globalCounterVariable +'">' + val + '</option>';
  globalCounterVariable += 1; 

  $('#categoryevent').append(opt);
  $('#categoryevent').val(val);
  $('#new-option-event').val('');

You will have to define a global veriable (actually outside of the scope of the .addevent handler is enough) that you initially will have to set to 3 (the number of hardcoded options). 您将必须定义一个全局验证(实际上超出.addevent处理程序的范围就足够了),您最初必须将其设置为3(硬编码选项的数量)。

Now you can do this: 现在您可以执行以下操作:

$('#categoryevent').on('change', function() {
      var selection = $(this).val(); //this is reference to the changed element, i.e. the select with Id 'categoryeveny'. 

      var html = '<div class="form-group"><label class="col-sm-2 control-label" for="templateId">Template ID: </label><div class="col-sm-8"><input class="form-control" id="templateId" name="templateId" readonly="readonly"></div></div>';

      $(html).find("input").val(selection);

      $(".actionConfig").empty();
      $('.actionConfig').append(html);
});

Note how I removed the need for all the if clauses, because you did the same logic each branch. 请注意,我如何消除了所有if子句的需要,因为每个分支的逻辑相同。 Simply parameterize whatever you want to inject and this is all you need. 只需参数化您想注入的任何东西,这就是您所需要的。

If you want to show different values for each selection (so different data than 1,2,3,... etc), you will have to either make that an option in the input where you create a new option, or hardcode generic logic in the change event, where you decide what content is shown based on the value of selection. 如果要为每个选择显示不同的值(因此与1,2,3,...等不同的数据),则必须在输入中创建新选项时将该选项设为一个选项,或对通用逻辑进行硬编码在change事件中,您可以根据选择的值决定显示哪些内容。

Here is the answer fiddle : DEMOanswer 这是答案提琴: DEMOanswer

$('#categoryevent').on("click", function(ev) {
        //  alert(ev.target.selectedIndex);
        var value = ev.target.selectedIndex;
        $('.actionConfig').empty();
        var z = $("#categoryevent option:selected").text();

        z = z.toLowerCase();

        var templateId = '<div class=form-group><label class="col-sm-2 control-label"for=templateId>Template ID: </label><div class=col-sm-8><input class=form-control id=templateId value="' + value + '" name=templateId readonly="readonly"></div></div>';
        $('.actionConfig').append(templateId);

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM