![](/img/trans.png)
[英]How to change url based on input type selected using javaScript/Jquery?
[英]How to change the input type based on the type stored in database?
我有一个表单供用户创建自定义问题。 用户需要引入问题以及字段类型(文本,长文本,复选框,选择菜单,单选按钮)来创建自定义问题:
<form method="post" class="clearfix" action="{{route('questions.store', ['conference_id' => $conference->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="question">Question</label>
<input type="text" class="form-control" name="question" id="question">
</div>
<div class="form-group">
<label for="type" class="text-heading h6 font-weight-semi-bold">Type of field</label>
<select class="form-control" name="type" id="type">
<option value="text">Text</option>
<option value="long_text">Long Text</option>
<option value="checkbox">Checkbox</option>
<option value="radio_btn">Radio Button</option>
<option value="select_menu">Select menu</option>
</select>
</div>
<div>
<input type="submit" class="btn btn-primary" value="Store"/>
</div>
</form>
在问题表的数据库中就像:
id question conference_id type
1 Whats your phone? 1 text
2 Want receive notifications? 1 radio_btn
3 .............. 1 checkbox
4 .............. 1 long_txt
5 .............. 1 select_menu
然后在registration.blade.php中我向用户显示自定义问题,以便他可以回答。 问题已经通过以下代码呈现给用户。 我怀疑的是如何根据存储在数据库中的问题类型更改输入类型。
你知道怎么做到这一点吗? 因为它始终是一个文本类型的问题。 但是,如果问题类型是例如复选框,则它应该将问题显示为复选框而不是输入类型文本。
@foreach($selectedType['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
<input type="text"
@if($customQuestion->pivot->required == "1") required @endif
class="form-control" name="participant_question[]">
<input type="hidden" name="participant_question_required[]"
value="{{ $customQuestion->pivot->required }}">
<input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
</div>
@endforeach
在您的Question
模型中,创建一个函数以输出所需的不同类型的输入。
例如
public function getHtmlInput($name = "", $id = "", $required = false, class="", val = "", $customtype=false)
{
switch ($this->type) {
case "text":
return "<input type='".($customtype?:"text")."' name='$name' id='$id' class='$class' value='$val'" . ($required?:"required") . ">";
case "checkbox":
...
}
}
然后在你的视图中你可以做$question->getHtmlInput(<params>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.