简体   繁体   English

如何在jQuery中获取选择框的值并提醒该值?

[英]How to get the value of select box in jquery and alert that value?

I have written a jquery code but it is not working for getting the selected option value of a select box. 我已经写了一个jQuery代码,但是它不能用于获取选择框的所选选项值。 it is alerting the message box not the value which is picked from the select box. 它警告消息框,而不是从选择框中选取的值。

<div class="form-group col-md-12 padd">
    <label for="exampleInputEmail1">Skill level</label>
    <select name="visa_status" id="visa_status" class="dropselectsec1">
        <option value="bfdgdfgdf">bfdgdfgdf</option>
        <option value="fdfdggf">fdfdggf</option>
        <option value="dfdf">dfdf</option>
    </select>
</div>

This is the select box. 这是选择框。

jquery code : jQuery代码:

<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("input").change(function(){
            var startDate = $("#start-date").val();
            var endDate = $("#end-date").val();
            $(".rentdate").html("Rental Date<span> From:"+startDate+" To:"+endDate);
        });

        $("select").change(function(){
            var selectedCountry = $(".dropselectsec1 option:selected").val();
            alert(selectedCountry);
        });
    });
</script>

Use $(this) instead. 使用$(this)代替。

$("select").change(function(){
    var selectedCountry = $(this).val();
    alert(selectedCountry);
});

$(this) points to the element on which you're binding the event handler. $(this)指向绑定事件处理程序的元素。 In your case it's selection box. 您的情况是选择框。

 $('select').change(function(){ var selectedCountry = $(this).val(); console.log(selectedCountry); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="form-group col-md-12 padd"> <label for="exampleInputEmail1">Skill level</label> <select name="visa_status" id="visa_status" class="dropselectsec1"> <option value="bfdgdfgdf">bfdgdfgdf</option> <option value="fdfdggf">fdfdggf</option> <option value="dfdf">dfdf</option> </select> </div> 

Please refer following jquery code. 请参考以下jQuery代码。 ".val()" function will useful to get value of selected dropdown. “ .val()”函数将有助于获取所选下拉列表的值。

$(document).ready(function() {
    $("#visa_status").change(function(){
        var selectedCountry = $(this).val();
        alert(selectedCountry);
    });
});

simply use this 只需使用这个

<div class="form-group col-md-12 padd">
                                  <label for="exampleInputEmail1">Skill level</label>
                                    <select name="visa_status" id="visa_status" class="dropselectsec1">

                                                    <option value="bfdgdfgdf">bfdgdfgdf</option>
                                                    <option value="fdfdggf">fdfdggf</option>
                                                    <option value="dfdf">dfdf</option>
                                                </select>
                                </div>

                                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script type="text/javascript">

$("#visa_status").change(function(){
    var selectedCountry = $(".dropselectsec1 option:selected").val();
    alert(selectedCountry);
});


</script>

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

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