在单选按钮选择上显示内容

时间:2018-07-11 16:30:17

标签: php laravel

我在update()中有代码来创建新证书或更新现有证书,并且它可以工作。

但是然后我希望当用户选择特定的注册类型(单选按钮)时,如何在文本区域中显示与所选注册类型(单选按钮)关联的证书的内容。

但“ alert(registrationTypeId);”无效。显示注册类型ID,但显示为“ $('#certificate_content')。val(certificate [registrationTypeId]);”不起作用。

CertificateController update():

 public function update(Request $request){
        $registrationType = RegistrationType::where('id', $request->registrationType);
        $certificate = $registrationType->certificate;

        // if no certificate exists for this type, create it
        if(!$certificate ) {
            $certificate = new Certificate();
        }

        $certificate->content = $request->certificate_content;
        $certificate->save();

        $registrationType->certificate_id = $certificate->id;
        $registrationType->save();

        Session::flash('success','Certificate configured with success.');

        return redirect()->back();
    }

视图中的表单:

    <form method="post" class="clearfix" action="{{route('certificates.update', ['conference_id' => $conference->id])}}" enctype="multipart/form-data">
    {{csrf_field()}}
    <div class="form-row">
        <div class="form-group col">
            <label>Certificate</label>
            @foreach($conference->registrationTypes as $registrationType)
                <div class="form-check">
                    <input
                            {{ (old('radiobutton') && old('radiobutton') == $rtype->id) ? 'checked' : '' }}
                            class="form-check-input radio" type="radio" name="registrationType"
                            value="{{ $registrationType->id }}" id="{{$registrationType->id}}">
                    <label class="form-check-label" for="exampleRadios1">
                        Certificate for the registration type "{{$registrationType->name}}"
                    </label>
                </div>
            @endforeach
        </div>
    </div>

    <div class="form-group">
        <label>Configure the certificate for the selected registration type</label>
        <textarea class="form-control" name="certificate_content" id="certificate_content" rows="3">{{ $certificate->content }} {{ old('certificate_content') }} </textarea>
    </div>
    <div>
        <input type="submit" class="btn btn-primary btn" value="Save"/>
    </div>
</form>

jQuery:

<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script type="text/javascript">

    var certificate = {};
    @foreach($onference->registrationTypes as $registrationType)
            @if(!$registrationType->certificate)
        certificate[{{ $registrationType->id }}] = '';
    @else
        certificate[{{ $registrationType->id }}] = '{{ $registrationType->certificate->content }}';
    @endif
    @endforeach

    $(function() {
        $('.radio').change(function() {
            var registrationTypeId = $('input[name=registrationType]:checked').val();
            alert(registrationTypeId); // shows the registration type id
            $('#certificate_content').val( certificate[registrationTypeId] );
        });

    });
</script>

@stop

1 个答案:

答案 0 :(得分:0)

使用tinymce初始化文本区域后,请使用以下命令:

tinymce.get('certificate_content').setContent('YOUR_CONTENT');

您也可以使用以下方法进行操作:

$(tinymce.get('certificate_content').getBody()).html('HTML_CONTENT');