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

如何在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!

图片转代码服务由CSDN问答提供 功能建议

我有一个由服务器端代码生成的php数组。 用户从我的下拉菜单中选择一个选项后,我想在输入字段中显示数组值。 请参阅我的代码:

  $(document).ready(function(){
 $('select [name =“job_number”]')。change(function(  ){
 var selectedVal = $(this).val(); 
 var test =“&lt;?php echo $ JNarray ['selectedVal'];?&gt;”; //问题是
 $(  'input [name =“project”]')。val(test); 
 //当用户从我的下拉菜单中选择
 //一个选项时,我希望在输入字段中将value1显示为值3。
 
  }; 
 
&lt;?php 
 $ JNarray ['job1'] = value1; 
 $ JNarray ['job2'] = value2; 
 $ JNarray ['job3'] = value3; 
?&gt;  ; 
 
&lt; form action ='project_manager'method ='post'&gt; 
&lt; input type ='text'name ='project'value ='当用户选择一个选项'/&gt;时显示value1到value3 \  n&lt; select name ='job_number'&gt; 
&lt; option value ='job1'&gt; job1&lt; / option&gt; 
&lt; option value ='job2'&gt; job2&lt; / option&gt; 
&lt; option value ='job3  '&gt; job3&lt; / option&gt; 
&lt; / select&gt; 
 
&lt; / form&gt; 
   
 
 

有任何想法吗?感谢您的帮助!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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);
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpou1935 2012-03-06 01:49

    Though, I don't recommened echoing php in js...

    var test=JSON.parse(<?php echo json_encode($JNarray['selectedVal']);?>); //where the problem is
    
    评论
    解决 无用
    打赏 举报
  • dsdeeaquu38538545 2012-03-06 01:54
    <select name='job_number'>
    <option value='<?php echo $JNarray['job1'] ?>'>job1</option>
    ...
    ...
    </select>
    
    <script>
    $(document).ready(function(){
    $('select[name="job_number"]').change (function () {
        $('input[name="project"]').val($(this).val()); 
    });
    </script>
    

    something like that.

    评论
    解决 无用
    打赏 举报
  • dqw70970 2012-03-06 02:03

    I agree with Trevor, however try

    "<?php echo $JNarray['"+selectedVal+"'];?>"
    

    Echoing PHP means your calling your JS from a PHP file, not a good approach dude!, The only thing I would pass is maybe a BASE_URL or FILE_UPLOAD_TYPE/SIZE, even those are ewwwwwwwy

    评论
    解决 无用
    打赏 举报
  • drs3925 2012-03-06 02:12
    1. You have to declare and populate your array before you use it on the jquery code. Move your <?php ?> code to the top.

    2. Then pass the whole php array to javascript, using JSON as Trevor suggested:

      $(document).ready(function(){
          var options = JSON.parse(<?php echo json_encode($JNarray);?>);
          $('select[name="job_number"]').change (function () {
              var selectedVal = $(this).val();
              var test=options[selectedVal]; //where the problem was
              $('input[name="project"]').val(test);  
          });
      });
      
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题