通过名称标签获取选择框选择的值 jquery

get selectbox selected value by name tag jquery

我有这样的表格

<label>Select country</label>  
    <select name="country">
        <option value="IND" selected="selected">India</option>
        <option value="MY">Malaysia</option>
        <option value="US">United states</option>
    </select>
</label>

我想使用 jquery 获取 selected 国家/地区的值。

这是我在 jquery 文档块中的代码: 我的 jquery 版本:jquery-1.10.1.js

alert($('select[name="country"]').find(":selected").val());
alert($('select[name="country"] option:selected').val());

我不喜欢用 ID 或 class 给 select 打电话。提前致谢。

我无法为国家 select-box 写 onChange。我需要对日期输入字段的模糊执行 ajax 请求 (followup_date)。

$('#followup_date').change(function(e) {

        if($(this).val()!='')
        {
            //$('.tmslot').not(this).attr('checked', false);        
            var slot = $(this).val();
            var country_id = $('select[name="country"] option:selected').val();

我的问题是 undefined for country_id.

根据文档,

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of select elements, it returns null when no option is selected and an array containing the value of each selected option when there is at least one and it is possible to select more because the multiple attribute is present.

您需要简单地使用 .val() 和 select 元素 jquery 对象来获得 selected 选项值:

$('select[name="country"]').val()

你可能需要这个Demo Here

alert($('select[name="country"]').val());
$('select[name="country"]').change(function(){    
    alert($(this).val());    
});

select 元素上使用 .val() 而不是在选定的 option 上使用 .val():

$('select[name="country"]').on('change', function(){    
    alert($(this).val());    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name="country">
        <option value="IND" selected="selected">India</option>
        <option value="MY">Malaysia</option>
        <option value="US">United states</option>
    </select>

如果你想获得选项:

alert($('select[name=country]').find(':selected').text());