douqian5920 2018-06-03 16:35
浏览 25
已采纳

你知道为什么选择菜单,输入类型文本和textarea表单元素没有出现?

I want to have the layout at left but it appears the layout at right. Do you know why is not appearing in the select menu, input type text and textarea?

enter image description here

In the Question model there is this getHtmlInput method() that is used in the view to generate the form fields:

In the registration.blade.php the questions are presented with the code below:

@if ($allParticipants == 0)
    @foreach($selectedRtype['questions'] as $customQuestion)

        <div class="form-group">
            <label for="participant_question">{{$customQuestion->question}}</label>
            @if($customQuestion->hasOptions())
                {!! $customQuestion->getHtmlInput(
                    $customQuestion->name,
                    $customQuestion->options,
                    ($customQuestion->pivot->required == '1'),
                    'form-control',
                    $customQuestion->type)
                !!}
            @endif
            <input type="hidden"
                   name="participant_question_required[]"
                   value="{{ $customQuestion->pivot->required }}">
            <input type="hidden"
                   value="{{ $customQuestion->id }}"
                   name="participant_question_id[]"/>
        </div>
    @endforeach
@endif

Generated html:

public function getHtmlInput($name = "", $options = "", $required = false, $class = "", $customtype=false)
    {
        $html = '';
        $html .= $customtype == 'select' ? "<select name='$name' class='$class' ".($required?:" required").">" : '';


        foreach($options as $option) {
            switch ($customtype) {
    case "text":
        $html .= " 
                <div class='form-group'>
                    <input type='text' name='participant_question' class='form-control'". ($required?:" required") . ">".
            '    <label class="form-check-label" for="exampleCheck1">' . $option->value. '</label>'.

            "</div>";

        $html .= "<input type='text' name='participant_question' value='".$option->value."' class='form-control'" . ($required?:" required") . ">";
        break;
    case "checkbox":
        $html .= " 
                <div class='form-check'>
                    <input type='checkbox' name='participant_question[]' value='".$option->value."' class='form-check-input'" . ($required?:" required") . ">".
                    '    <label class="form-check-label" for="exampleCheck1">' . $option->value. '</label>'.

                "</div>";
        break;
    case "radio_btn":
        $html .= " 
                <div class='form-check'>
                    <input type='radio' name='participant_question[]' value='".$option->value."' class='form-check-input'" . ($required?:" required") . ">".
            '    <label class="form-check-label" for="exampleCheck1">' . $option->value. '</label>'.

            "</div>";
        break;
    case "select_menu":
        $html .= "<option value='".$option->value."'>";
        break;
    case "textarea":
        $html .= "
          <div class='form-group'>
                <textarea name='participant_question' class='form-control' rows='3'" . ($required?:" required") . ">"
                    . $option->value .
                "</textarea>
        </div>";

        break;
            }
        }

        $html .= $customtype == 'select' ? "</select>" : '';

        return $html;
    }

https://jsfiddle.net/za2LpgL7/

  • 写回答

1条回答 默认 最新

  • donglikuang8145 2018-06-03 20:19
    关注

    Yes. You've got a couple of items that are stopping this from working.

    First is not a code issue - you simply don't have any options on the object. The code

    @if($customQuestion->hasOptions())
    

    fails since the customQuestion has no options attached. Thus, the code for the input boxes never gets entered. Create a test object an attach the options for sure. This may or may not work, but it will get you to the next step of seeing if the getHtmlInput() function works.

    On that note, the fiddle you set up is showing that the getHtmlInput() function is either missing some code or the object is missing the type parameter. The function appears to be entering options for the select box, but no select tag. You need to initialize the select in your function ( add the proper HTML <select></select> tags into the function ). I see you've made arrangement for this by the final method parameter, $customtype=false, but it appears this is also not set coming in to the function. If you look at the fiddle, we see:

    <label for="participant_question">Select menu question</label>
    <option value="opt1"></option><option value="opt1">
    

    The <select> is never entered. I don't think your code is too far off (though I don't know how the options got in the fiddle if there were none to pass the if check), I think you need to test your objects to see what they include -- make sure they have options, make sure they have that $customtype and that it is set as select for the first round. If the customQuestion object has the right parameters this will allow you to properly test.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler