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

你知道为什么选择菜单,输入类型文本和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.

    点赞 评论

相关推荐 更多相似问题