简体   繁体   中英

What's wrong in this script?

When I submit the form and I try to get the Roomsno input value its showing like this-

Array ( [0] => 1,2 ); why???

How can I send it so that it will come as a real array means like this- 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>

As the value is in array format so Rather than setting the value directly use jason.stringfy- Ex.

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

And when i try to get the value i use json.decode and the value will come as array and we can use it normally as we use array. The array will come like- Array([0]=>1[1]=>2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM