download2565 2015-12-09 04:43
浏览 40
已采纳

如何获取输入数组值

I have a form with multiple input array id's

for example:

    <div>
        <input name='set1[]'>
        <input name='set2[]'>
        <input name='set3[]'>
    </div>
    <div>
        <input name='set1[]'>
        <input name='set2[]'>
        <input name='set3[]'>
    </div>

The problem I have is if I leave out one of my sets my array key position is '0' instead of '1'. Use below as an example:

    <div>
        <input name='set1[]'>
        <input name='set2[]'>
    </div>
    <div>
        <input name='set1[]'>
        <input name='set2[]'>
        <input name='set3[]'>
    </div>

When set 3 isn't in the first div, set 3 key value is '0' when I need it to be '1'.

I am using Jquery to append each 'set'. Any ideas on how to get the 'set3[]' to be key value '1'?

here is my Jquery:

$(document).ready(function() {

    var max_fields = 9; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap_ex1"); //Fields wrapper
    var add_button = $(".add_field_button_ex1"); //Add button 
 
    $(document).on("click", ".add_field_button_ex1", function (e) { //on add input button click 
    var num = $(this).parents().children('.setsdiv').length;
    var index = $(this).parent().parent().parent().siblings().children().index('#myinputwkout');
    alert(index);
        e.preventDefault();

        if ($(this).parents().children('.setsdiv').length < max_fields) { //max input box allowed
            $container=$(this).parent().parent().parent();
            $container.append('<div id="setsdiv" class="col-xs-12 text-center setsdiv"><div class="col-xs-3 col-sm-4 nopad"><input id="set" class="form-control input-sm setinput text-center col-xs-6 set" type="text" name="sets'+ num +'[]" placeholder="Weight" pattern="\\d*"></div><div class="col-xs-3 col-sm-4 nopad"><input id="reps" class="form-control input-sm setinput text-center col-xs-6 reps" type="text" name="reps'+ num +'[]" placeholder="Reps" pattern="\\d*"></div><div class="col-xs-6 col-sm-4"><a class="btn btn-xs btn-default remove_field pull-right">Remove</a><a class="same btn btn-xs btn-default" href="#">Same</a></div></div>'); //add input box
            $container.find('.set:last').focus();

}
});

    $(document).on("click", ".remove_field", function (e) { //user click on remove text
        e.preventDefault();
        $(this).closest('.setsdiv').remove();
    })

    $(document).on("click", ".same", function (e) { //user click on same text
        e.preventDefault();
        //Get
    var set = $(this).parent().parent().prev().find('#set').val();
    var rep = $(this).parent().parent().prev().find('#reps').val();

        //Set
    $(this).parent().siblings().children('#set').val(set);
    $(this).parent().siblings().children('#reps').val(rep);

    })

});

</div>
  • 写回答

1条回答 默认 最新

  • duanhan9479 2015-12-09 05:27
    关注

    SOLVED!

    I added to my Jquery....

            var index = $(this).parent().parent().parent().parent().prevAll('.form-group').length;
    var index = index - 1;
    

    Then I put into my 'append()'

        sets'+ num +'[' + index + ']
    

    This did the trick!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题