简体   繁体   English

javascript以动态添加文本框

[英]javascript to dynamically add a textbox

I am trying to dynamically add a text box based on the selection of dropbox. 我试图根据投寄箱的选择动态添加文本框。 When the user selects 'other' a text box gets generated asking them to explain the other. 当用户选择“其他”时,将生成一个文本框,要求他们解释另一个。 The user can dynamically add mutiple dropboxes, resulting in multiple text boxes if other is selected value in dropbox. 用户可以动态添加多个保管箱,如果在保管箱中选择其他值,则可以创建多个文本框。 Every generated dropbox has a unique name which gets read and placed in id of dynamically generated textbox. 每个生成的保管箱都有一个唯一的名称,该名称将被读取并放置在动态生成的文本框的id中。

The problem i am facing is that when there are multiple dropboxes and the first dropbox selection is something other that 'Other' and second dropbox value is other, the text box generated is placed in front of the first dropbox where it should be placed in front of the dropbox relevant to it. 我面临的问题是,当有多个保管箱并且第一个保管箱选择是“ Other”以外的其他东西,而第二个保管箱的值是“ other”时,生成的文本框被放置在第一个保管箱的前面,应该放在前面与之相关的保管箱。

The html code is as follows for the dropbox: html代码如下所示:

<div id="container">

    <label id="rightlabel"for="dropbox1"></label>
    <span>
      <select id="frequency" onclick="getData(this, name)" name="dropbox[4fb103e3-06e7-4c88-8836-73b855968478]"></select>
      <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="dropbox[4fb103e3-06e7-4c88-8836-73b855968478]"></span>
    </span>
<div id="hiddenothertexbox"></div>
</div>

Javascript is as follows: Javascript如下:

function getData(title, i) {

var value = title.options[title.selectedIndex].value; 
var y = i.replace(/-/g, '')
$('#hiddenother').attr('id', y);

if (value == 'Other') {

    str = '<label id="leftlabel">if other, please specify</label><span><input id="textboxid" class="text-box single-line" type="text" value="" name="textbox ></input></span>';       
    $('#'+y).html(str);         
}
else {
    str = '';
    $('#'+y).html(str);       
}
 }

teh javascript gets the names of the dropbox and replaces the id of 'hiddenanothertextbox' with that id so its unique. javascript获取了保管箱的名称,并将“ hiddenanothertextbox”的ID替换为该ID,以使其具有唯一性。 I have an idea of the problem, i think its because when the user does not click teh first dropbox the id of 'hiddenanothertextbox' does not change for the first dropbox and when another dropbox is added and the value is changed, the hiddenanothertextbox for first dropox value changes adding it in front of first not second. 我有一个问题的想法,我想是因为当用户没有单击第一个保管箱时,第一个保管箱的“ hiddenanothertextbox”的ID不会更改,并且当添加了另一个保管箱且值已更改时,第一个保管箱的hiddenanothertextbox dropox值更改将其添加到第一个而不是第二个前面。 I am struggling to achieve teh required result. 我正在努力实现所需的结果。

UPDATED JAVASCRIPt 更新的Javascript

function getData(title, i) {

var value = $(title).val();
var y = i.replace(/-/g, '');
$('#hiddenother').attr('id', y);

if (value == 'Other') {

    str = '<label id="leftlabel">if other, please specify</label><span><input id="textboxid" class="text-box single-line" type="text" value="" name="textbox"" ></input></span>';       
    $(title).after(str);         
}
else {

    $(title).nextUntil('#textboxid').remove();       
}
}

Working Demo 工作演示

Use this code snippet. 使用此代码段。

var flag = 0;
function getData(title, i) {

var value = $(title).val();

  var y = i.replace(/-/g, '');
  $('#hiddenother').attr('id', y);

  if (value == 'Other' ) {
      if(flag == 0){
          flag=1;
          str = '<label id="leftlabel">if other, please specify</label><span><input id="textboxid" class="text-box single-line" type="text" value="" name="textbox"></input></span>';       
          $(title).after(str); 
      }        
  }
  else {
    flag=0;
    $(title).nextUntil('#textboxid').remove();       
  }
}

Html : HTML:

<select id="frequency" onchange="getData(this, name)" name="dropbox[4fb103e3-06e7-4c88-8836-73b855968478]">

Changes : 变化 :

  • var value = $(title).val() gives you the correct value var value = $(title).val()为您提供正确的值
  • Use onchange="getData(this, name)" instead of onclick=".." 使用onchange="getData(this, name)"代替onclick=".."
  • In variable str : name="textbox" did not have ending " 在变量strname="textbox"没有结尾的"
  • use .after() or .append() instead of .html() 使用.after().append()代替.html()

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

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