在脚本标记内执行一个函数

时间:2016-03-31 06:22:36

标签: jquery laravel-5

{!! Form::open(array('url'=>'calendars','method'=>'POST', 'class'=>'eventform')) !!}

<table>
    <tr>
        <th >{!! Form::label('titles', 'Title') !!}</th>

        <td >{!! Form::text('title') !!}<br><br></td>
    </tr>

            @if($errors->has('title'))
                <tr><td></td>
                <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$errors->first('title')}}</ul></td>
                </tr>
            @endif



    <tr>
    <tr>

        <td> <b>{!! Form::label('Types', 'Type') !!}</b></td>

        <td>


            {!! Form::select('type', array('type' => 'type','Orange' => 'Orange', 'Red' => 'Red','Green' => 'Green'), 'type', ['id' => 'type']) !!}
            {!! Form::hidden('color','', ['id' => 'color']) !!}
        </td>

    </tr>

        <th >{!! Form::label('dates', 'Event Date') !!}&nbsp;&nbsp;&nbsp;</th>
        <td >{!! Form::input('date', 'eventDate', $value = null, $options = array()) !!}<br><br></td>
     </tr>
            @if($errors->has('eventDate'))
        <tr><td></td>
            <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$errors->first('eventDate')}}</ul></td>
        </tr>


            @endif
    <tr>
        <th >{!! Form::label('Venue', 'Venue') !!}</th>
        <td >{!! Form::text('venue') !!}<br><br></td>

    </tr>
    @if($errors->has('venue'))
        <tr><td></td>
            <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$errors->first('venue')}}</ul></td>
        </tr>


    @endif
    <tr>
        <th >{!! Form::label('Time', 'Time') !!}</th>
        <td >{!! Form::input('time', 'time', $value = null, $options = array()) !!}<br><br></td>
    </tr>
    @if($errors->has('time'))
        <tr><td></td>
            <td><ul class="alert alert-danger" style="width: 250px;height: 40px"> {{$errors->first('time')}}</ul></td>
        </tr>


    @endif
    @if(Session::has('error1'))
        <tr><td></td>
        <td><ul class="alert alert-danger" style="width: 250px;height: 40px">{!! Session::get('error1') !!}</ul></td>
        </tr>
    @endif

    <tr>
    <td colspan='2' align='center'>{!! Form::submit('Add', array('class'=>'Add')) !!} </td>
    </tr>
    </table>


        {!! Form::close() !!}

        <script>
            $(function(){
                $('#type').on('change', function(){
                    $('#color').val($('#type').val());
                });
            });
        </script>

我该如何执行此脚本代码?我应该在哪里打电话?这是在视图blade.php文件中。表格也在那里。我需要在提交按钮上调用它吗?以上代码是根据选定的下拉框值

更改隐藏的输入值

1 个答案:

答案 0 :(得分:0)

您不需要手动运行它。只要它已加载,当您更改ID为类型的下拉框时,它就会起作用。

试试这个(而不是document.ready ...):

....
    <script>
        $(function(){
            $('#type').on('change', function(){
               $('#color').val($('#type').val());
            });
        });
    </script>

修改 这是示例代码(完整的HTML,它的工作原理)。使用选择后,按下按钮显示隐藏输入的值。复制并将其保存为html文件。

<html>
<head>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<script>
        $(function(){
            $('#type').on('change', function(){
               $('#color').val($('#type').val());
            });
        });
    </script>
    <select id='type'>
        <option>Value1</option>
        <option>Value2</option>
        <option>Value3</option>
    </select>
    <input name='foo' type='hidden' id='color' value='Value1'/>

    <button onclick='alert($("#color").val())'>Show Hidden Value</button>
</body>
</html>

如果您没有从服务器端接收它,那么您应该检查隐藏的输入名称。

修改

我复制了你的代码,并在这里创建了一个laravel项目

https://laravello-yerlibilgin.c9users.io/YourProjectName/server.php

,还添加了一个javascript警报。只要值发生变化,您就可以看到隐藏值已成功设置。更改后,单击添加按钮以查看效果。

服务器端处理程序是这样的:

 Route::post('/calendars', function () {
     $val =  Input::get('color');
     return $val;
 });

此代码成功运行并接收隐藏输入的值。请检查您的服务器端。