简体   繁体   English

如何使用动态生成的html输入类型实时填充div

[英]how to live populate div using dynamically generated html input types

i'm trying to populate div with select option but i don't really now where to start... 我正在尝试使用选择选项填充div,但我现在真的不知道从哪里开始...

i have some code to live edit the "title" of the div, but now i want to add to a specific div his option... 我有一些代码可以实时编辑div的“标题”,但是现在我想将其选项添加到特定的div ...

Here's the code that i have for now: 这是我现在拥有的代码:

 var rooms = $("#howmanyrooms").val();
            var roomcounter = 1;
            $(".design-your-system-page-playground-container").show();
            for (var i = 0; i < rooms; i++) {
                //    $("<div class='appendeddiv'>Room-" + roomcounter++ + "</div>").appendTo(".housecontainer");
                //    $("<span>Room-" + roomcounter + " name</span>&nbsp;<input type='text' placeholder='name' id='room-" + roomcounter + "-id'></div></br>").appendTo(".infoncontainer");
                //
                $("<div class='design-your-system-page-rooms targetDiv_" + roomcounter + "'>Room-" + roomcounter + "</div>").appendTo(".design-your-system-page-house");
                $("<span>Room-" + roomcounter + " name</span>&nbsp;<input type='text' placeholder='name' id='room-" + roomcounter + "-id' class='textInput' lang='targetText_" + roomcounter + "'>&nbsp<select>Heating<option value='radiator'>Radiator</option><option value='underfloor'>Underfloor</option><option value='electric'>Electric</option></select>&nbsp;<select class='design-your-system-number-of-radiator-select'><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></select>&nbsp;<span>Do you want the room to be smart (footprint) ?<input type='radio' name='smart-yes' value='smart-yes'>Yes</input>&nbsp;<input type='radio' name='smart-no' value='smart-no'>No</input></div></br>").appendTo(".design-your-system-page-edit-room-container");
                roomcounter++;
            };
            if ($('.design-your-system-page-house').find('.design-your-system-page-rooms').length) {
                $("#buttonaddrooms").hide();
            }

            $("input.textInput").on("keyup", function () {
                var target = $(this).attr("lang").replace("Text", "Div");
                $("." + target).text($(this).val());
            });

as you can see, when i click the button, i'll append to the parent as many child divs as the value typed into the textbox and i also create the same number of "row" containing the name and other option (two select and a radio) i'm already able to live edit the name of the ralative div, but now i want to add to that div also the other options 如您所见,当我单击按钮时,我会将与在文本框中键入的值一样多的子div附加到父项,并且我还将创建相同数量的“行”,其中包含名称和其他选项(两个select和收音机)我已经可以实时编辑ral div的名称,但是现在我想在该div中添加其他选项

here a jsfiddle to help you understand what i have and what i want: http://jsfiddle.net/3cyST/ 这是一个jsfiddle,可帮助您了解我拥有的东西和想要的东西: http : //jsfiddle.net/3cyST/

if is not clear please tell me. 如果不清楚,请告诉我。 thanks 谢谢

please check this fiddle: 请检查小提琴:

i made your target variable global to be reusable, i also added a class for your first select element which is selecting 我提出的target变量全球受到重用,我还添加了类为第一个select元件,其selecting

ive updated it and it now appends the value of your test onchange using: ive更新了它,现在它使用以下命令附加了onchange测试的值:

 $("select.selecting").on("change", function () {
                $("." + target).append($(this).val());
            });

you can work for the rest now. 您现在可以继续工作。

EDIT ( for the question of OP on the comment ) : 编辑关于评论的OP问题 ):

to get value of radio button i'll give you 2 ways : 为了获得单选按钮的价值,我将为您提供2种方法:

in Javascript : Javascript

if (document.getElementById('ID_OF_RADIO').checked) {
  rate_value = document.getElementById('ID_OF_RADIO').value;
}

in jQuery : jQuery

$("input[name=RADIO_NAME]:checked").val();

give the select an id, then use 给选择一个ID,然后使用

$("#id").on("change",function(){
    console.log(this.value);
    //whatever you want to do with the value
})

...same for the radio buttons and other options...also note that the radio buttons shouldn't have different names: ...与单选按钮和其他选项相同...也请注意,单选按钮不应使用不同的名称:

<input type='radio' name='radio_{put id here}' value='yes'>Yes</input>
<input type='radio' name='radio_{put id here}' value='no'>No</input>

another thing for the readabillity of the code: try using a template....just put a <noscript> with an id in the code...use some distinctive syntax to put placeholders in it, and replace them at runtime: 代码可读性的另一件事:尝试使用模板...。只需在代码中放入带id的<noscript> ...使用一些独特的语法将占位符放入其中,并在运行时替换它们:

HTML: HTML:

<noscript id="template">
    RoomName: <input type="text" id="roomName_%ROOMID%" />
    Do you want...
    <input type='radio' name='radio_%ROOMID%' value='yes'>Yes</input>
    <input type='radio' name='radio_%ROOMID%' value='no'>No</input>
</noscript>

JS: JS:

for (var i = 0; i < rooms; i++) {
    var tplcode = $("#template").html();
    tplcode = tplcode.replaceAll("%ROOMID%",roomcounter);
    $($.pareHTML(tplcode)).appendTo(".design-your-system-page-edit-room-container");

    $("input[name='radio_"+roomcounter+"']").on("change",function(){
        console.log("user wants:" + $("input[name='radio_"+roomcounter+"'][checked]").val())
    });
    roomcounter++;
}



// these functions help replacing multiple occurances
String.prototype.replaceAll = function(find,replace){
    return this.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

//escapse all regEx chars, so the string may be used in a regEx
function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

Fiddle: http://jsfiddle.net/3cyST/4/ 小提琴: http//jsfiddle.net/3cyST/4/

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

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