简体   繁体   English

从 select 框中获取所选值

[英]get the selected value from select box

I have a problem when I want to show the type of input from the select element当我想显示来自 select 元素的输入类型时遇到问题

This is the select element in my view这是我认为的 select 元素

<select id="type" name="type_id" class="form-control" required>
    @foreach($type as $row)
    <option value="{{$row->id}}">{{$row->label}}</option>
    @endforeach
</select>

If the user selects the type of question number they got the type of their input number, if they select string they get the type of the input string for example如果用户选择问题编号的类型,他们得到输入编号的类型,如果他们是 select 字符串,他们得到输入字符串的类型,例如

<h4 class="card-title"  id="rep">Reponse : {{$i + 1}}</h4>
<input type="text"  id="rep_for_oui" required class="form-control" placeholder="Reponse question {{$i + 1 }}" name="rep_{{$i}}" />
<input type="text" id="rep_for_string" required class="form-control" placeholder="Reponse question {{$i + 1 }}" name="rep_{{$i}}" />
<input type="number"  id="rep_for_number" required class="form-control" placeholder="Reponse question {{$i + 1 }}" name="rep_{{$i}}" />

And this is my Javascript这是我的 Javascript

$('#rep').hide();
$('#rep_for_oui').hide();
$('#rep_for_string').hide();
$('#rep_for_number').hide();
var e = document.getElementById("type_id");
var value = e.options[e.selectedIndex].value;
$('#type').onchange(function () {
    alert(value);
    if (value == "2") {
        $('#rep').show();
        $('#rep_for_oui').show();
    }
});

Instead of:代替:

var e = document.getElementById("type_id");

Do the following:请执行下列操作:

var e = document.getElementById("type");

For getting Selected Value you can use like为了获得选定的值,您可以使用类似

For me,为了我,

var selectedType = $('#type').val()

returned null.返回 null。

I had to use我不得不使用

var selectedType = $('#type option:selected').val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM