简体   繁体   English

获取 HTML 选择元素的预选值

[英]Get pre-selected value of an HTML select element

Or through example, how can one retrieve "c" via JavaScript or jQuery no matter if the user changed it?或者举个例子,无论用户是否更改了“c”,如何通过 JavaScript 或 jQuery 检索它?

<select id="select-menu">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c" selected>C</option>
    <option value="d">D</option>
</select>

I tried the following, however, it returns the value of the currently selected menu.我尝试了以下操作,但是,它返回当前选定菜单的值。 I suppose I can take a similar approach which runs upon page load and save the value, but expect there is a more "proper" way to do so.我想我可以采用类似的方法,在页面加载时运行并保存值,但希望有一种更“正确”的方法来做到这一点。

$('#select-menu > option').each(function(i){
    if(this.selected) {
        console.log(this.value)
         $("#submit").val(this.value);
         return false;
    }
})

There is no better way.没有更好的办法。 A select is meant to be user-modifiable.选择意味着用户可修改。

If you want to retain the original value you could save it on load:如果要保留原始值,可以在加载时保存它:

$(document).ready(function() {
    var initialValue = $("#select-menu").val();
});

You could also write it in a hidden input:你也可以把它写在一个隐藏的输入中:

<input type="hidden" name="initial-select-menu" value="c">

This way has the benefit that the value will be submitted along with the rest of the form.这种方式的好处是值将与表单的其余部分一起提交。 Might be preferable in some cases.在某些情况下可能更可取。

Or you could stuff that initial value in a data attribute:或者您可以将该初始值填充到data属性中:

<select id="select-menu" data-initial="c">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c" selected>C</option>
    <option value="d">D</option>
</select>

Use jQuery .attr() to get initial value:使用 jQuery .attr() 获取初始值:

 function get_initial(){ $('#select-menu > option').each(function(i){ if($(this).attr('selected')) { console.log('initial: ' + this.value) } if(this.selected) { console.log('current: ' + this.value) } }) } $('.initial').on('click', get_initial);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="select-menu"> <option value="a">A</option> <option value="b">B</option> <option value="c" selected>C</option> <option value="d">D</option> </select> <button class="initial">check initial</button>

You could save the value on an html5 data-* attribute:您可以将值保存在 html5 data-* 属性上:

<select id="select-menu" data-default="c">

And get it with并得到它

$("#select-menu").attr("data-default");

The following code exhibits various examples related to getting of values from select fields using JavaScript.以下代码展示了与使用 JavaScript 从选定字段获取值相关的各种示例。

Working Snippet工作片段

 const selectMenu = document.getElementById("select-menu"); var selectedIndex = selectMenu.options[selectMenu.selectedIndex].value; /*Option 1*/ selectMenu.onchange = function() { selectedIndex = selectMenu.options[selectMenu.selectedIndex].value; } /*Option 2*/ /*selectMenu.addEventListener("change", function(){ selectedIndex = selectMenu.options[selectMenu.selectedIndex].value; })*/ /*Option 3*/ /*function updateSelectedIndex() { selectedIndex = selectMenu.options[selectMenu.selectedIndex].value; }*/ document.writeln(selectedIndex);
 <!-- Option 3 --> <!-- <select id="select-menu" onchange="updateSelectedIndex"> --> <select id="select-menu"> <option value="a">A</option> <option value="b">B</option> <option value="c" selected>C</option> <option value="d">D</option> </select>

Try this for the appropriate cases:在适当的情况下试试这个:

 let preselectedOption = $(targetDropDown).find('option[selected]');
 let currentOption = $(targetDropDown).find('option:selected');

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

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