var answered = ["john","jay","mark"];

 function tick(){

   $("#btn_wys").on('click', function(){

     var name = $('input#name').val();
     var num = $('input#num').val();

     if ($.trim(name)!= ''){
       $.post('wys.php', {name: name, num: num},function(data){
          $('div#name-data').text(data);
     });
    }
 }


我将如何传递数组“已回答”,以便使用ajax在wys.php上读取变量名称,数字和已回答。

在wys.php内部的查询将回显1或0。

附加问题:如果wys.php回显1,是否可以在“已回答”数组中添加新元素?如果是,怎么办?

最佳答案

我想这就是你所需要的

var answered = ["john","jay","mark"];

//run function
tick(answered);

function tick(answered){ // pass in function
  answered.push('Smith'); //insert new
  alert(answered[0]); // get the value number 1 in array
  alert(answered[1]); // get the value number 2 in array
  alert(answered[2]); // get the value number 3 in array
  alert(answered[3]); // get the value number 4 in array
  alert(answered.length); // how many values in array
}


Working Demo

通过PHP

{name: name, num: num , answered : answered}


并在PHP中将其读取为数组

<?php
 print_r($_POST['answered']);
?>

10-04 20:20