dongpao1921 2012-03-06 01:46
浏览 52
已采纳

如何在javascript中回显php数组?

I have an php array that is generated by server-side code. I want to show the array value in my input field after user pick an option from my dropdown menu. Please see my code:

$(document).ready(function(){
$('select[name="job_number"]').change (function () {
    var selectedVal = $(this).val();
    var test="<?php echo $JNarray['selectedVal'];?>"; //where the problem is
    $('input[name="project"]').val(test);  
//I want show value1 to value 3 in my input field when user picks 
//an option from my dropdown menu. 

});

<?php 
   $JNarray['job1']=value1;
   $JNarray['job2']=value2;
   $JNarray['job3']=value3;
?>

<form action='project_manager' method='post'>
<input type='text' name='project' value='show value1 to value3 when user picks an option' />
<select name='job_number'>
<option value='job1'>job1</option>
<option value='job2'>job2</option>
<option value='job3'>job3</option>
</select>

</form>

Any thoughts? Thanks for the help!

  • 写回答

5条回答 默认 最新

  • douzuqin3467 2012-03-06 02:07
    关注

    Here's a cleaner way to do it without having to set the array to a variable. Use a data attribute that jQuery reads with $.data()

    HTML:

      <option value='job1' data-job-val="<? echo $arrayvalue ?>">job1</option>
    

    JS:

    $('select[name="job_number"]').change (function () {
        var test=$(this).find('option:selected').data('job-val');
        $('input[name="project"]').val(test);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数