duanbianweng5353 2017-09-14 11:33
浏览 73
已采纳

复选框与PHP的有序选择数组

<form method='post' action=''>
 <label for="x1">X1</label>
 <input id="x1" name="checkbox[]" type="checkbox" value="x1" />

 <label for="x2">X2</label>
 <input id="x2" name="checkbox[]" type="checkbox" value="x2" />

 <label for="x3">X3</label>
 <input id="x3" name="checkbox[]" type="checkbox" value="x3" />

 <label for="x4">X4</label>
 <input id="x4" name="checkbox[]" type="checkbox" value="x4" />

 <button type='submit' name='submit'>Submit</button>
</form>

In this form How can i get the selection order of the checkboxes inside the php array

Just if i submitted the form after clicking x4 > x2 > x3 > x1

i want to get ordered in the array as it is selected checkbox[x4, x2, x3, x1]

The normal array i get is as it is ordered checkbox[x1, x2, x3, x4].

  • 写回答

2条回答 默认 最新

  • dongmao3148 2017-09-14 11:40
    关注

    Check the clicked order using jquery

    push the value to array on change event.if is checked push to the array.is unchecked value remove from array

    Join the array with , add the value to hidden input .you could post this value to server

    var arr=[]
    $('input[type=checkbox]').on('change', function() {
      if ($(this).is(':checked')) {
        arr.push($(this).val())
      } else {
        var r =arr.indexOf($(this).val())
        arr.splice(r, 1)
    }
    $('input[type=hidden]').val(arr.join(',')) //for  server use
    console.log(arr)
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form method='post' action=''>
      <label for="x1">X1</label>
      <input id="x1" name="checkbox[]" type="checkbox" value="x1" />
    
      <label for="x2">X2</label>
      <input id="x2" name="checkbox[]" type="checkbox" value="x2" />
    
      <label for="x3">X3</label>
      <input id="x3" name="checkbox[]" type="checkbox" value="x3" />
    
      <label for="x4">X4</label>
      <input id="x4" name="checkbox[]" type="checkbox" value="x4" />
    <input type="hidden" name="clickedorder" >
      <button type='submit' name='submit'>Submit</button>
    </form>

    Updated

    change the label value depend on clicking order

    var arr=[]
    $('input[type=checkbox]').on('change', function() {
      if ($(this).is(':checked')) {
        arr.push($(this).val())
      } else {
        var r =arr.indexOf($(this).val())
        arr.splice(r, 1)
    }
    $('input[type=checkbox]').each(function(a){
    //console.log(arr.indexOf($(this).val()))
      if(arr.indexOf($(this).val())>-1){
         $(this).prev('label').text($(this).val().toUpperCase()+'-'+(arr.indexOf($(this).val())+1))
      }else{
        $(this).prev('label').text($(this).val().toUpperCase())
      }
    })
    $('input[type=hidden]').val(arr.join(',')) //for  server use
    
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form method='post' action=''>
      <label for="x1">X1</label>
      <input id="x1" name="checkbox[]" type="checkbox" value="x1" />
    
      <label for="x2">X2</label>
      <input id="x2" name="checkbox[]" type="checkbox" value="x2" />
    
      <label for="x3">X3</label>
      <input id="x3" name="checkbox[]" type="checkbox" value="x3" />
    
      <label for="x4">X4</label>
      <input id="x4" name="checkbox[]" type="checkbox" value="x4" />
    <input type="hidden" name="clickedorder" >
      <button type='submit' name='submit'>Submit</button>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行