English 中文(简体)
将php数组传递给jquery函数
原标题:pass php array to jquery function

我有一个页面,上面有一张表格,可以填写客户信息。我将以前的客户机数据存储在php数组中。有了下拉列表,用户可以用我存储的数据填写表格。

我有一个jquery函数,当值发生变化时会触发,在该函数中,我想用我存储的数据(在我的php数组中)更新表单值。

问题是我不知道如何将php数组传递给jquery函数,有什么想法吗??

这是我填充数组的方式:

$contador_acompanantes = 0;

foreach ($acompanantes->Persona as $acomp) 
{
  $numero = $acomp->Numero;
  $apellidos = $acomp->Apellidos;
  $nombre = $acomp->Nombre;

  $ACOMPANANTES[$contador_acompanantes] = $ficha_acomp;
  $contador_acompanantes++; 
}

获取了我选择的对象:

<select name="acompanante_anterior" id="acompanante_anterior">
<option value="1" selected>Acompañante</option>
<option value="2">acompanante1</option>
<option value="2">acompanante2</option>
</select>

这是我的jquery函数的代码(它在同一个.php页面中)

<script type="text/javascript">
$(document).ready(function() 
{ 
$( #acompanante_anterior ).on( change , function() 
    {

    });
});
</script>
最佳回答
var arrayFromPHP = <?php echo json_encode($phpArray); ?>;
$.each(arrayFromPHP, function (i, elem) {
    // do your stuff
});
问题回答

试试这个。。

<script type="text/javascript">
    $(document).ready(function() 
    { 
    $( #acompanante_anterior ).on( change , function() 
        {
            my_array = new Array();

            <?php foreach($array as $key->val)?>
                my_array[ <?php echo $key?> ] =  <?php echo $val;?> ;
            <?php endif; ?>

        });
    });
    </script>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签