javascript - 如何用 php 数组填充 javascript?

标签 javascript php jquery html laravel

我有以下 Javascript,它允许我在单击按钮时生成选择输入框并将选择框附加到 .cardday div:

        <script>
            $('input[name="queue"]').click(function(){
                $("<select name='select[]'>"+
                    "<option value='volvo'>Volvo</option>"+
                    "<option value='saab'>Saab</option>"+
                    "<option value='mercedes'>Mercedes</option>"+
                    "<option value='audi'>Audi</option>"+
                    "</select>").appendTo('.cardDay');
            })
</script>

我还有一个 PHP 数组,其中包含需要放入选择框中的值。该数组称为 $exerciseList

如何才能拥有它,以便当选择框添加到卡片 div 时,它会填充 PHP 数组 $exerciseList 键和值?

按要求提供完整文件:

    @extends('layouts.app')

@section('content')

<div class="card" style="padding:10px;">
        {!! Form::open(['action' => ['ExercisePlansController@store'], 'method' => 'POST']) !!}
        <center><h3 style="margin-top:10px;margin-bottom:20px;">Create Training Plan
                {{ Form::submit('Create this Plan', ['class' => 'btn btn-success', 'style' => 'float:right;align:right;margin-right:10px;']) }}
                </h3></center>
    <hr style="margin:10px;"></hr>


    <div class="form-group">
            {{ Form::label('title', 'Name') }}
            {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Exercise Plan Name E.g Advanced Bulking']) }}
    </div>
    <div class="form-group">
        {{ Form::label('plan', 'Calender Plan') }}</br>
        <div class="col-md-2" style="padding:0px 10px 0px 0px;flex:0%;">
            <div class="cardDay" style="padding:10px;background-color:#c7e0fc;">

                <h3 style="margin-bottom:3px;">
                    <center>Monday</center>
                    <hr style="margin:10px;"></hr>
                </h3>
                <input class="btn btn-primary" style="font-family:Helvetica;padding:10px;margin:5px 0px 5px 0px;width:100%;" name="queue" value="Add Exercise">
                    <div class="panel-group">
                        <div class="panel panel-default">
                          <div class="panel-heading">
                            <h4 class="panel-title">
                              <a data-toggle="collapse" href="#collapse1001">New Exercise</a>
                            </h4>
                          </div>
                          <div id="collapse1001" class="panel-collapse collapse in">
                                <div class="panel-body">

                                    <div class="form-group">    
                                        {{ Form::label('exerciseList', 'Exercise') }} <br/>
                                        {{ Form::select('exerciseList[]', $exerciseList) }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('description', 'Exercise Description') }} <br/>
                                            <a class="card-link"  href="./exercise/">Exercise Details</a><br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('reps', 'Number of Reps') }} <br/>
                                            {{ Form::text('reps[]', "") }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('sets', 'Number of Sets') }} <br/>
                                            {{ Form::text('sets[]', '') }} <br/>
                                    </div>
                                    <div class="form-group">    
                                            {{ Form::label('weight', 'Weight (kg)') }} <br/>
                                            {{ Form::text('weight[]', '') }} <br/>
                                    </div>     
                                </div>

                          </div>
                        </div>
                      </div>
        </div>
            <center>
            {{ Form::hidden('_method', 'PUT') }}

            </center>
        {!! Form::close() !!}
        "{{ Form::label('exerciseList', 'Exercise') }} <br/>"+
        "{{ Form::select('exerciseList[]', $exerciseList) }} <br/>"+
        <script>
            $('input[name="queue"]').click(function(){
                $("<select name='select[]'>"+
                    "<option value='volvo'>Volvo</option>"+
                    "<option value='saab'>Saab</option>"+
                    "<option value='mercedes'>Mercedes</option>"+
                    "<option value='audi'>Audi</option>"+
                    "</select>").appendTo('.cardDay');
            })
        </script>
@endsection

最佳答案

让服务器生成 Javascript:

<script>
    $('input[name="queue"]').click(function(){
    $("<select name='select[]'>"+
    <?php foreach ($exerciseList as $value){ ?>
        "<option value='<?php echo $value; ?>'><?php echo $value; ?></option>"+
    <?php } ?>
    "</select>").appendTo('.cardDay');
    })
</script>

因此,当浏览器加载 Javascript 时,代码中将包含您的 PHP 变量值。

关于javascript - 如何用 php 数组填充 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49864081/

相关文章:

javascript - knockout JS : replace specific field value in multi dimentional array

javascript - 如何检查对象中是否存在相同的字符串?

php - 使用 PHP 从文件和目录数组中删除文件

javascript - 将字符串 1k 字符串转换为 1000 数字

javascript - jQuery 过滤具有多个属性的元素

javascript - 是否可以将 HTML 输入字段变量传递给自定义 Angularjs 过滤器?

c# - ASP.Net 404 大型 XMLHttpRequest 文件上传

php - 排除 foreach 表中的列而不丢失数据

php - 如何将用逗号分隔的 SQL 列数据分离为单个值,然后对这些值进行计数

javascript - rails 4 : JS doesn't work when I follow links