[英]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.