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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。