简体   繁体   English

如何使用Jquery基于选择输入以表单形式动态添加DIV

[英]How to add DIV's dynamically in a form based on a select input with Jquery

 var beneficiarioDiv = $('<div class="beneficiario-wrap"><label for="nombre_bene">Nombre y Apellido</label><input type="text" name="nombre_bene" value=""><label for="nombre_bene">Cedula beneficiario</label><div><select name="cedulabene_tipo"><option value="Venezolano" selected>V </option><option value="Extranjero">E </option></select> <input type="text" name="cedulabene_no" value="" maxlength="8"></div><label for="edad_bene">Edad beneficiario</label><input type="text" name="edad_bene" value=""></div>'); $('#beneficiarios').change(function() { var beneficiarios = $('#beneficiarios :selected').val(); // this gives me the value that the user selected <div class="beneficiarios-wrapper"></div> // this is the div where I want to append the number of divs selected by the user }); 
  <label for="beneficiarios">Cantidad de beneficiarios</label> <select name="beneficiarios" id="beneficiarios"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> 

I have been hitting my head cuz I can't think on a way to get this to work ( I'm new with Jquery ) 我一直在打我的头,因为我想不出一种方法来使它工作(我是Jquery的新手)

So.. I have a select input field in a form with number options.. I want to check which of the options the user selected and add a DIV the number of times the user selected ( from 0 to 10 ) and if the user selects a number lower than the one he previously chose delete the DIVs previously added and add the ones he now chose (if he selects number 3 add 3 DIVs, if he now selects 2 because he was just mistaken then just show 2 instead of the 3) 所以..我在一个带有数字选项的表单中有一个选择输入字段。.我想检查用户选择了哪个选项,并向用户添加了DIV次数(从0到10),以及用户是否选择一个比他先前选择的数字低的数字,删除先前添加的DIV,然后添加他现在选择的数字(如果他选择数字3,则添加3个DIV,如果他现在选择2,因为他只是被误认为,则只显示2而不是3)。

I know how to get the number the user selected with the .change event listener and the :selected.val() method.. my problem is appending the divs that number of times the user selected, and deleting them if the user selects a number lower than. 我知道如何获取使用.change事件侦听器和:selected.val()方法选择的用户的号码。我的问题是将divs附加到用户选择的次数,如果用户选择了数字,则将其删除低于。

Please i don't know how to make this work! 请我不知道如何使这项工作!

In your change function, first clear the wrapper and then append the divs in a loop 在您的change函数中,首先清除包装器,然后将div附加到循环中

 var beneficiarioDiv = '<div class="beneficiario-wrap"><label for="nombre_bene">Nombre y Apellido</label><input type="text" name="nombre_bene" value=""><label for="nombre_bene">Cedula beneficiario</label><div><select name="cedulabene_tipo"><option value="Venezolano" selected>V </option><option value="Extranjero">E </option></select> <input type="text" name="cedulabene_no" value="" maxlength="8"></div><label for="edad_bene">Edad beneficiario</label><input type="text" name="edad_bene" value=""></div>'; $('#beneficiarios').change(function() { var beneficiarios = $('#beneficiarios :selected').val(); // this gives me the value that the user selected $('.beneficiarios-wrapper').empty(); while (beneficiarios--) { $('.beneficiarios-wrapper').append(beneficiarioDiv); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for="beneficiarios">Cantidad de beneficiarios</label> <select name="beneficiarios" id="beneficiarios"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <div class="beneficiarios-wrapper"></div> 

You could just append the divs in a loop: 您可以将div附加到循环中:

var b = = $('#beneficiarios :selected').val();
while(b--) {
   $(".beneficiarios-wrapper").append("<div>"+b+"</div>")     
}          

You can clear the div on change event. 您可以清除change事件中的div。 Then use a for loop to append div 然后使用for循环附加div

  const divToAppend = '<div>test</div>'; let number = $('#beneficiarios').val(); $('#beneficiarios').on('change', function () { $('#placeholder').html(''); number = $(this).val(); for (let i = 0; i < number; i++) { $('#placeholder').append(divToAppend); } }); 
  <label for="beneficiarios">Cantidad de beneficiarios</label> <select name="beneficiarios" id="beneficiarios"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <div id="placeholder"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

暂无
暂无

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

相关问题 如何使用jquery基于选择标签的输出向输入标签动态添加REQUIRED属性? - how to dynamically add REQUIRED attribute to input tags based on output of select tag using jquery? 如何根据 Jquery 中的值向子 div 动态添加和删除 class - How to add and remove class dynamically to child div based on value in Jquery 如何基于文本输入过滤div并一起选择输入 - How to filter div's based on text input and select inputs together 使用Jquery根据文本动态添加div - Use Jquery to add a div dynamically based on text 当我使用 jquery 动态添加新表单输入时,Select2 输入不起作用 - Select2 input doesn't work when I add a new form input dynamically with jquery 使用jquery从其他表单动态填充新的表单字段,这些字段预先填充了用户的输入? - Dynamically Add New form fields pre-populated with user's input from another form using jquery? How to filter/ add CSS class to an HTML table based on a user's input to HTML form using Java script of JQUERY - How to filter/ add CSS class to an HTML table based on a user's input to HTML form using Java script of JQUERY jQuery显示基于选择值动态隐藏div - jquery show hide div based on select value dynamically 如何使用on()通过Jquery向动态添加的div中添加日期选择器 - How to add a datepicker to a dynamically added div with Jquery with on() 如何“添加更多”可以基于选择值显示表单输入字段的选择 - How to “Add more” the select that can show form input fields based on select value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM