简体   繁体   English

打开一个长<select>第一次单击下拉列表时,下拉到特定选项

[英]open a long <select> dropdown to specific option when the dropdown is first clicked

I have a large dropdown list containing possible answer like this:我有一个很大的下拉列表,其中包含如下可能的答案:

<select name="mySelect" id="mySelect" required="" >
        <option value="-100"  > -100% </option>
        <option value="-99"  > -99% </option>
        <option value="-98"  > -98% </option>
        <option value="0"  > 0% </option>
        <option value="101" selected = "" hidden = ""> ----- </option>

going from -100 to 100. When the dropdown is first clicked, I would like it to open (and select) at a prespecified value, ie at 0.从 -100 到 100。当第一次单击下拉列表时,我希望它以预先指定的值(即 0)打开(并选择)。

I have tried this:我试过这个:

$('#mySelect').one('focus', function() {
    $(this).children('option[value=0]').prop('selected',true);
});

and this works well in Chrome, but it doesn't work in Firefox, which is a problem for me.这在 Chrome 中运行良好,但在 Firefox 中不起作用,这对我来说是个问题。 (edit) As mentioned in the comments, in Firefox above script selects the correct option, but dropdown is not scrolled to that value. (编辑)如评论中所述,在 Firefox 中,上面的脚本选择了正确的选项,但下拉列表不会滚动到该值。

Help with a working solution in both Firefox and Chrome would be greatly appreciated.在 Firefox 和 Chrome 中提供工作解决方案的帮助将不胜感激。

Here is a fiddle of the problem: https://jsfiddle.net/monni/an4guvse/28/这是问题的一个小提琴: https : //jsfiddle.net/monni/an4guvse/28/

I don't want to show any "real" value before the dropdown is clicked在单击下拉列表之前,我不想显示任何“真实”值

Given this statement, in the comments, what you're attempting to do is not workable.鉴于此声明,在评论中,您尝试做的事情是行不通的。 The actions available on an options list within a select are vey limited, and this behaviour is non-standard. select中的选项列表中可用的操作非常有限,而且这种行为是非标准的。

I'd argue it's even counter intuitive and will confuse your users and cause more problems than it solves.我认为它甚至违反直觉,会让您的用户感到困惑并导致比它解决的问题更多的问题。

What you're attempting to achieve seems like it could be better solved by using a range input:您试图实现的目标似乎可以通过使用range输入更好地解决:

 $('#foo').on('input', e => $(e.target).next().text(e.target.value + '%'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <input type="range" min="-100" max="100" value="0" id="foo" /> <span></span>

Please try to create a snippet within your question next time.下次请尝试在您的问题中创建一个片段。 Here's is a ( plain js ) minimal reproducable example .这是一个(纯 js最小可重现示例 It uses a focusin handler, and event delegation .它使用focusin处理程序和事件委托 To change the selected option, you can simply set the value of the selector to the desired value.要更改所选选项,您只需将选择器的值设置为所需值即可。

fwiw: setting a select value on focus may be annoying for users. fwiw:在焦点上设置选择值对用户来说可能很烦人。

 document.addEventListener(`focusin`, handle); // create the selector dynamically document.body.insertAdjacentHTML(`beforeend`, `<select id="mySelect"> <option>------</option> ${[...Array(201)].map((v, i) => `<option value="${i-100}"}>${i-100}%</option>`).join(``)} </select>`); document.querySelector(`#mySelect`).value = `100`; function handle(evt) { if (evt.target.id === `mySelect`) { evt.target.querySelector(`[value='-50']`).scrollIntoView(); return evt.target.value = `-50`; } }

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

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