如何从动态输入对创建js对象?

时间:2017-05-26 14:59:59

标签: javascript jquery html

我对它非常凌乱,无法找到正确的方法。 我有一个按钮,它创建一对输入,一个是类型,另一个是“值”。

我想将他们的文本值收集到整齐的小物体中以便进一步处理。但这并不重要。这是它的样子: enter image description here

这就是我到目前为止所得到的

 try {
        var typy = {};
        var wartosc;
        var arrtypow = [];
        $('.atribinput').each(function () {

                if ($('.atribtext').hasClass('select2-offscreen')) {
                    wartosc = $(this).select2('data')['text'];
                } else {
                    wartosc = $(this).val();
                }


                 typy = {
                      typ:$('.atribtype').select2('data')['text'],
                      wartosc:wartosc
                 };

            }
        );
        console.log(typy);
    }
    catch(err) {
        return null;
    }

但它会返回奇怪的东西,如:

Object { typ: "Data otrzymania", wartosc: text() }

老实说,我不知道出了什么问题,我该怎么办?如果有可能的话。

哦是的和类:.atribinput是所有动态输入(左和右),。atribtext是正确的输入和.atribtype左边的输入。也许是因为他们分享了一个班级?

编辑:

根据要求,我添加了HTML标记。

<div class="form-group attribBoxes">
<div class="attribBox">
    <input name="data[ProjectAttribute][0][project_attribute_type_id]" value="2" id="ProjectAttribute0ProjectAttributeTypeId" type="hidden">
    <div class="form-group">
        <label for="ProjectAttribute0Value" class="col-md-4 control-label">Data otrzymania</label>
        <div class="col-sm-5">
            <input name="data[ProjectAttribute][0][value]" class="form-control summarytrigger required datepicker" empty="Wpisz wartość " id="ProjectAttribute0Value" data-validate="required" type="text">
        </div>
    </div>
</div>
<div class="attribBox">
    <input name="data[ProjectAttribute][1][project_attribute_type_id]" value="3" id="ProjectAttribute1ProjectAttributeTypeId" type="hidden">
    <div class="form-group">
        <label for="ProjectAttribute1Value" class="col-md-4 control-label">Termin odpowiedzi</label>
        <div class="col-sm-5">
            <input name="data[ProjectAttribute][1][value]" class="form-control summarytrigger required datepicker" empty="Wpisz wartość " id="ProjectAttribute1Value" data-validate="required" type="text">
        </div>
    </div>
</div>
<div class="attribBox">
    <div class="inputdefault3" style="display: none">
        <input name="data[ProjectAttribute][3][value]" class="form-control summarytrigger atribinput atribtext required" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text">
    </div>
    <div class="clear"></div>
    <div class="row">
        <div class="col-md-4 col-xs-12" style="margin-left: 8%">
            <div class="form-group">
                <div class="select2-container projectattributetypeid3 atribinput summarytrigger atribtype" id="s2id_autogen9">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Data otrzymania</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen10" type="text">
                </div>
                <input name="data[ProjectAttribute][3][project_attribute_type_id]" class="projectattributetypeid3 atribinput summarytrigger atribtype select2-offscreen" tabindex="-1" type="text">
            </div>
        </div>
        <div style="margin-left: 4%" class="col-md-4 col-xs-12 inputchange3">
            <div class="form-group">
                <input name="data[ProjectAttribute][3][value]" class="form-control summarytrigger atribinput atribtext required datepicker" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text"> </div>
        </div><a style="margin-left: 2%; margin-top: 5px;" class="btn btn-danger removeatributesrow">-</a></div>
</div>
<div class="attribBox">
    <div class="inputdefault4" style="display: none">
        <input name="data[ProjectAttribute][4][value]" class="form-control summarytrigger atribinput atribtext required" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text">
    </div>
    <div class="clear"></div>
    <div class="row">
        <div class="col-md-4 col-xs-12" style="margin-left: 8%">
            <div class="form-group">
                <div class="select2-container projectattributetypeid4 atribinput summarytrigger atribtype" id="s2id_autogen11">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Województwo</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen12" type="text">
                </div>
                <input name="data[ProjectAttribute][4][project_attribute_type_id]" class="projectattributetypeid4 atribinput summarytrigger atribtype select2-offscreen" tabindex="-1" type="text">
            </div>
        </div>
        <div style="margin-left: 4%" class="col-md-4 col-xs-12 inputchange4">
            <div class="form-group">
                <div class="select2-container atribtext form-control required" id="s2id_ProjectAttribute4Value">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Podkarpackie</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen17" type="text">
                </div>
                <select id="ProjectAttribute4Value" class="atribtext form-control required select2-offscreen" name="data[ProjectAttribute][4][project_attribute_type_dicts_id]" data-validate="required" tabindex="-1">
                    <option value="">-- Wybierz z listy ---</option>
                    <option value="2">Małopolska</option>
                    <option value="3">Podkarpackie</option>
                    <option value="4">Śląskie</option>
                </select>
            </div>
        </div><a style="margin-left: 2%; margin-top: 5px;" class="btn btn-danger removeatributesrow">-</a></div>
</div>
<div class="attribBox">
    <div class="inputdefault5" style="display: none">
        <input name="data[ProjectAttribute][5][value]" class="form-control summarytrigger atribinput atribtext required" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text">
    </div>
    <div class="clear"></div>
    <div class="row">
        <div class="col-md-4 col-xs-12" style="margin-left: 8%">
            <div class="form-group">
                <div class="select2-container projectattributetypeid5 atribinput summarytrigger atribtype" id="s2id_autogen13">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Zainteresowania</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen14" type="text">
                </div>
                <input name="data[ProjectAttribute][5][project_attribute_type_id]" class="projectattributetypeid5 atribinput summarytrigger atribtype select2-offscreen" tabindex="-1" type="text">
            </div>
        </div>
        <div style="margin-left: 4%" class="col-md-4 col-xs-12 inputchange5">
            <div class="form-group">
                <div class="select2-container atribtext form-control required" id="s2id_ProjectAttribute5Value">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Muzyka</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen18" type="text">
                </div>
                <select id="ProjectAttribute5Value" class="atribtext form-control required select2-offscreen" name="data[ProjectAttribute][5][project_attribute_type_dicts_id]" data-validate="required" tabindex="-1">
                    <option value="">-- Wybierz z listy ---</option>
                    <option value="5">Muzyka</option>
                    <option value="6">Sport</option>
                    <option value="7">Kino</option>
                </select>
            </div>
        </div><a style="margin-left: 2%; margin-top: 5px;" class="btn btn-danger removeatributesrow">-</a></div>
</div>
<div class="attribBox">
    <div class="inputdefault6" style="display: none">
        <input name="data[ProjectAttribute][6][value]" class="form-control summarytrigger atribinput atribtext required" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text">
    </div>
    <div class="clear"></div>
    <div class="row">
        <div class="col-md-4 col-xs-12" style="margin-left: 8%">
            <div class="form-group">
                <div class="select2-container projectattributetypeid6 atribinput summarytrigger atribtype" id="s2id_autogen15">
                    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1"> <span class="select2-chosen">Pilna data</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow"><b></b></span></a>
                    <input class="select2-focusser select2-offscreen" id="s2id_autogen16" type="text">
                </div>
                <input name="data[ProjectAttribute][6][project_attribute_type_id]" class="projectattributetypeid6 atribinput summarytrigger atribtype select2-offscreen" tabindex="-1" type="text">
            </div>
        </div>
        <div style="margin-left: 4%" class="col-md-4 col-xs-12 inputchange6">
            <div class="form-group">
                <input name="data[ProjectAttribute][6][value]" class="form-control summarytrigger atribinput atribtext required datepicker" empty="Wpisz wartość " id="ProjectAttribute2Value" data-validate="required" type="text"> </div>
        </div><a style="margin-left: 2%; margin-top: 5px;" class="btn btn-danger removeatributesrow">-</a></div>
</div>

如您所见,我希望左侧的值为右侧的值,值为数组或对象或对象数组中的值。

如果可以的话,请提供帮助。

0 个答案:

没有答案