选择输入标签 - 单击时选择中间日期

发布于 2024-10-17 00:26:49 字数 124 浏览 1 评论 0原文

我目前拥有标准选择输入多年。目前,当单击时,它会显示一长串从 1900 年到 2000 年的年份列表,需要您向下滚动。

有没有办法让它在单击时出现在中间,以便您可以向上或向下滚动?

我希望这是有道理的!

I currently have a standard select input for years. Currently, when clicked, it presents a long list of years from 1900-2000, that require you to scroll down.

Is there a way to make it appear in the middle, when clicked, so that you can scroll up or down?

I hope this makes some sense!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

年少掌心 2024-10-24 00:26:49

使用 selected 属性

 <select name="years">
    <option>1900</option>
    <option>1901</option>
    <option>1902</option>
    <option>1903</option>
    <option>1904</option>
    <option selected="selected">1905</option>
    <option>1906</option>
    <option>1907</option>
    <option>1908</option>
    <option>1909</option>
    <option>1910</option>
    </select>

示例:http://jsfiddle.net/tnU4K/

use selected attribute

 <select name="years">
    <option>1900</option>
    <option>1901</option>
    <option>1902</option>
    <option>1903</option>
    <option>1904</option>
    <option selected="selected">1905</option>
    <option>1906</option>
    <option>1907</option>
    <option>1908</option>
    <option>1909</option>
    <option>1910</option>
    </select>

example: http://jsfiddle.net/tnU4K/

莫言歌 2024-10-24 00:26:49

对于下拉

就我个人而言,我不会打扰。大多数用户都知道,无论如何,他们可以在聚焦 中选择选项。

For a drop down <select> element? Not in a standardized, cross browser way. Some browsers may (untested) allow you to manipulate the scrollTop property of the select element while the list is visible, but I wouldn't count on it. The only way to start in the middle is have the middle year selected by default.

Personally, I wouldn't bother. Most users know that they can type whilst focusing a <select> element anyway — I rarely use the mouse to select an option in a drop-down <select>.

心房的律动 2024-10-24 00:26:49

在默认选择的中间(可能是 1950)附近设置一个值。当有人单击选择时,所选值将可见,因此您可以向上或向下滚动。

Make a value somewhere around the middle (maybe 1950) selected by default. When someone clicks the select the selected value will be visible, so you can scroll up or down.

述情 2024-10-24 00:26:49

你总是可以疯狂地使用 jquery 做这样的事情:

//get number of options
var optCount = $('#example option').length;
//divide by 2 and round up for odd numbers
var midPoint = Math.round(optCount/2);
//select that index item
$("#example option:eq(midPoint)").attr("selected", "selected");

我还没有测试过,但它应该可以工作。

You could always go hog wild and do something like this using jquery:

//get number of options
var optCount = $('#example option').length;
//divide by 2 and round up for odd numbers
var midPoint = Math.round(optCount/2);
//select that index item
$("#example option:eq(midPoint)").attr("selected", "selected");

I haven't tested but it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文