簡體   English   中英

這個腳本有什么問題?

[英]What's wrong in this script?

當我提交表單並嘗試獲取 Roomno 輸入值時,其顯示如下-

Array ( [0] => 1,2 ); 為什么???

我怎樣才能發送它以便它作為一個真正的數組出現,就像這樣 - Array([0]=>1 [1]=>2)

<input type="hidden" class="form-control" name="Roomsno" id="Roomsno" required>

<script>
    var rmidarray = []; //  new Array()
    var rmnoarray = [];

    $('.roomtype').change(function() {

      roomss_id = $(this).attr('data-id');
      no_room = $(this).val();

      var check = rmidarray.includes(roomss_id);

      if (check == true) {
        // alert('hi')
        index = rmidarray.indexOf(roomss_id);
        // alert(index);
        rmnoarray.splice(index, 1, no_room);

        // rmnoarray[index].push(no_room);
      } else if (check == false) {
        // alert('by');
        rmidarray.push(roomss_id);
        rmnoarray.push(no_room);
      } else {
        alert('No rooms Selected!!!')
      }


      $("#Roomsno").val(rmnoarray);

    });

   </script>

由於值是數組格式,所以不要直接使用 jason.stringfy-Ex 設置值。

$("#Roomsno").val(rmnoarray); //Instead of this one
$('#Roomsno').val(JSON.stringify(rmnoarray)); //This one worked for me

當我嘗試獲取值時,我使用 json.decode 並且該值將作為數組出現,我們可以像使用數組一樣正常使用它。 數組會像 - Array([0]=>1[1]=>2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM