开发者

multiple values for <options> in <select> list HTML

I'm having a little trouble attacking this problem and wondered if anyone could push me in the right direction. What I would like to do is have a list that's have two components, a label and value. So in essence I would like a list that looks like a table with the ability to scroll up and down (since my size is preset and I have to display all the values), I'm just not sure how to approach it.

My first approach was just to embed a table with all my values inside of a div with overflow:scroll. Which was great until I brought it into Mobile Opera, where overflow doesn't work.

For a quick fix I threw my labels and values into separate select lists, which does work and look good. Except I don't want to have to scroll them independently, i.e. the labels weren't matching their values if I scrolled one and not the other.

A quick and dirty approach I have now is just combining them as strings into a single option, but it does not really look good and I would ideally like to text-align:left / text-align:right for better viewing.

What I have so far:

    var statusVarLabel = ["label 1","label 2","label 3","label 4","label 5","label 6","label 7","label 8","label 9","label 10","label 11","label 12","label 13","label 14","label 15","label 16","label 17","label 18","label 19","label 20"];
    var statusVarInfo = ["variable 1","variable 2","variable 3","variable 4","variable 5","variable 6","variable 7","variable 8","variable 9","variable 10","variable 11","variable 12","variable 13","variable 14","variable 15","variable 16","variable 17","variable 18","variable 19","variable 20"];
    function getStatus(){
        var varContainer = document.getElementById("varContainer");
        varContainer.size = statusVarLabel.length;
        tabSpacing = 0;
        for (var i=0; i < statusVarLabel.length; i++) {
            if (tabSpacing < statusVarInfo[i].length)
                tabSpacing = statusVarInfo[i].length;
        };
        for (var i=0; i < statusVarLabel.length; i++) {
            var check = 0;
            for (var j=0; j < varContainer.length; j++) {
                if ( statusVarLabel[i] == varContainer.options[j].text.slice(0,statusVarLabel[i].length)){
                    check = 1;
                    break;
                }
            };              
            var stringSpacing = "";
            for (var k=0; k < (tabSpacing-statusVarInfo[i].length)+5; k++) {
                stringSpacing = stringSpacing + " ";
            };
            if (check == 0)
                addOption(varContainer, statusVarLabel[i] + stringSpacing + statusVarInfo[i], i);
        };          
    }

Just wondering if a开发者_开发问答nyone out there has run into the same problem. I'm still a bit new to the HTML game, is there a way to embed a table or div within a list?


In select list elements, you can only have option and optgroup elements. Nothing else is supported.

Ideally, you could use a table with a custom JavaScript scrollbar widget; this would allow you to work around Mobile Opera's problems with overflow. But a fully functional scrollbar is quite complex and it takes time to implement. Of course you can search for a jQuery plugin or something similar, but you might have to resort to a partially functional scrollbar, for example one that does not handle dragging. Just keep in mind that when they see a scrollbar, certain users may expect that it will work exactly like a native scrollbar.

Otherwise, if you want to keep it simple, it is possible to "couple" two or more select lists so that when you scroll one, the others scroll synchronously.

For example, using the onscroll event and the scrollTop property:

HTML

<div id="varContainer">
    <select id="seVarLabels" style="text-align: left"></select>
    <select id="seVarInfos" style="text-align: right"></select>
</div>

JavaScript

var varContainer = document.getElementById("varContainer");

var seVarLabels = document.getElementById("seVarLabels");
seVarLabels.size = statusVarLabel.length;

var seVarInfos = document.getElementById("seVarInfos");
seVarInfos.size = statusVarInfo.length;


seVarLabels.onscroll =  function () {
    seVarInfos.scrollTop = this.scrollTop;
};
seVarLabels.onclick =  function () {
    seVarInfos.selectedIndex = this.selectedIndex;
}
seVarInfos.onscroll =  function () {
    seVarLabels.scrollTop = this.scrollTop;
}
seVarInfos.onclick = function () {
    seVarLabels.selectedIndex = this.selectedIndex;
}

for (var i=0; i < statusVarLabel.length; i++) {
    addOption(seVarLabels, statusVarLabel[i], i);
    addOption(seVarInfos, statusVarInfo[i], i);
}

It worked in Firefox, but I could not test it on Mobile Opera.

An inconvenient of this approach is that you will have a vertical scrollbar between the two lists; however, using CSS it should be possible to position the second list so that it hides that scrollbar by overlapping the first list by the scrollbar's width.


I am getting a little lost are you trying to do the following ?

 <select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
 </select>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜