简体   繁体   English

使用javascript获取多个字段的值并将其添加到表中

[英]Using javascript to get the values of multiple fields and add them to a table

I'm creating a script that takes the values of the fields and places them into a table for verification. 我正在创建一个脚本,该脚本接受字段的值并将其放入表格中进行验证。 The thing is though, the user is able to add multiple fields(up to 5) and each field has its own id. 事实是,用户可以添加多个字段(最多5个),每个字段都有自己的ID。 So, when a user inputs the values I want my script to take all the values from that field(even if they've added) and pass them into the table. 因此,当用户输入值时,我希望我的脚本从该字段中获取所有值(即使已添加),并将它们传递到表中。 How would I go about doing this? 我将如何去做呢? User adds one 'set', clicks add chart 用户添加一个“设置”,单击添加图表 用户添加一个“集合”

User adds another 'set'(up to 5). 用户添加另一个“集合”(最多5个)。 添加另一组

When the user has added all the 'sets' he/she wants, They appear in the Jeppeson Charts section. 用户添加了他/她想要的所有“设置”后,它们将显示在“ Jeppeson图表”部分中。 在此处输入图片说明
I need to get these values 我需要获得这些价值

<div id="charts">
    <h3>Jeppeson Charts</h3>
    <fieldset>
        <label>Airport Identifier</label>
        <input type="text" name="air_id_1" id="air_id_1" />
    </fieldset>
    <fieldset>
        <label>Chart Identifier</label>
        <input type="text" name="chart_id_1" id="chart_id_1" />
    </fieldset>
    <fieldset>
        <label>Chart Description</label>
        <input type="text" name="chart_desc_1" id="chart_desc_1" />
    </fieldset>
</div>

Allows the user to add up to 5 different charts. 允许用户添加多达5个不同的图表。

var jep_num = 1;
    $("#add_chart").on("vclick", function(ev){
        jep_num += 1;
        if(jep_num <= 5){
            $("#charts").append("<fieldset><input type='text' name='air_id_" +jep_num +"' id='air_id_" +jep_num +"' /></fieldset><fieldset style='margin-left: 0.45%'><input type='text' name='chart_id_" +jep_num +"' id='chart_id_" +jep_num +"' /></fieldset><fieldset style='margin-left: 0.4%'><input type='text' name='chart_desc_" +jep_num +"' id='chart_desc_" +jep_num +"' /></fieldset>").trigger("create");
        }
        ev.preventDefault();
    })

Table where the charts need to be added 需要添加图表的表

<div class="full">
    <div class="header">JEPPESON CHARTS</div>
</div>
<div class="half">
    <div class="half">
        <div id="air_id_field" class="table_field"></div>
    </div>
    <div class="half">
        <div id="chart_id_field" class="table_field"></div>
    </div>
</div>
<div class="half">
    <div class="full">
        <div id="chart_desc_field" class="table_field"></div>
    </div>
</div>

I hope I made sense with my rambling. 我希望我的漫谈是有道理的。 If not, let me know and I'll try to clarify. 如果没有,请告诉我,我会尽力澄清。 I also know how to do this using MySQL and PHP, and I was hoping to avoid doing so with this particular feature. 我也知道如何使用MySQL和PHP做到这一点,我希望避免使用此特殊功能。 But, if I need to I will. 但是,如果需要的话,我会的。

You should add a class to each field and the added ones too. 您应该为每个字段添加一个类,并且也要添加一个类。 Also you need to wrap all three related fieldset in one div. 另外,您需要将所有三个相关的字段集包装在一个div中。

<div id="charts">
     <h3>Jeppeson Charts</h3>

    <div class="charts-fieldset">
        <fieldset>
            <label>Airport Identifier</label>
            <input class="air-input" type="text" name="air_id_1" id="air_id_1" />
        </fieldset>
        <fieldset>
            <label>Chart Identifier</label>
            <input class="chart-input" type="text" name="chart_id_1" id="chart_id_1" />
        </fieldset>
        <fieldset>
            <label>Chart Description</label>
            <input class="chart-desc-input" type="text" name="chart_desc_1" id="chart_desc_1" />
        </fieldset>
    </div>
</div>

Then you can go through all of your sets one by one and get the values like that and then do what you want with them: 然后,您可以一一遍历所有集合,并获得类似的值,然后对它们进行所需的处理:

$('.charts-fieldset').each(function () {
    var air = $('.air-input', $(this)).val();
    var chart = $('.chart-input', $(this)).val();
    var chartDesc = $('.chart-desc-input', $(this)).val();
});

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

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