如何自动设置下拉列表的大小? [英] How to automatically set the size of a drop-down list?

查看:115
本文介绍了如何自动设置下拉列表的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Greasemonkey编写一个脚本,该脚本将自动根据选择选项的数量设置下拉列表的大小(可以有多个...),但是由于我有限的JavaScript知识,我不知道真的不知道该怎么做.

I'm trying to write a script in Greasemonkey that will automatically set the size of Drop-down lists (there can be more than one...) on the number of selection options, but with my limited JavaScript knowledge I don't really know how to do this.

主题示例:

<select name="z_pos_id">
<option value="2463">Option A</option>
<option value="2609">Option B</option>
<option value="3013">Option C</option>
</select>

<select name="z_pos_id">
<option value="140">Option AA</option>
<option value="3038">Option AB</option>
<option value="3519">Option AC</option>
<option value="2645">Option AD</option>
</select>

所需输出示例:

<select size="3" name="z_pos_id">
<option value="2463">Option A</option>
<option value="2609">Option B</option>
<option value="3013">Option C</option>
</select>

<select size="4" name="z_pos_id">
<option value="140">Option AA</option>
<option value="3038">Option AB</option>
<option value="3519">Option AC</option>
<option value="2645">Option AD</option>
</select>


因此,<select name="z_pos_id">应替换为:


So, <select name="z_pos_id">should be replace with this:

<select size="*the number of options*" name="z_pos_id">

谢谢大家的帮助!

推荐答案

这是一个完整的脚本.这应该是很自我解释的:

Here's a complete script. It should be fairly self explanatory:

// ==UserScript==
// @name     _Auto-size select selects.
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    none
// ==/UserScript==

var posIdSelects = document.querySelectorAll ("select[name=z_pos_id]");
for (var J = posIdSelects.length - 1;  J >= 0;  --J) {
    var numOpts  = posIdSelects[J].getElementsByTagName ("option").length;
    posIdSelects[J].setAttribute ("size", numOpts);
}

这篇关于如何自动设置下拉列表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆