如何从listbox listitem中获得价值 [英] How to get value from listbox listitem

查看:120
本文介绍了如何从listbox listitem中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的firefox插件中,我有一个< listbox> 。当我左键单击框中的项目时,我希望能够使用JavaScript函数。



现在,当我点击一个列表项时,我的函数不会被调用,因为我把它放在我的事件监听器的onLoad调用中:

  var myListBox = document。的getElementById( myListBoxID); 
myListBox.addEventListener(click,function(event){
var target = event.target;
while(target& target.localName!=listitem){
target = target.parentNode;
}
if(!target){
return; //事件目标不是列表项目
}
alert (target); //返回空白
alert(target.id); //返回空白
alert(target.getAttribute(value)); //返回空白
alert(target .getAttribute(text)); //返回空白
alert(target.getAttribute(id)); //返回空白

var targetid = document.getElementById(target。 id);
alert(targetid); //返回null
},false);
},

xul会像这样:

 < listbox id =listbox1> 
< listcols />< listcol flex =1/>< listcol flex =1/>< / listcols>
< listitem>< listcell class =column1label =label1value =value1< / listcell>< listcell label =cell1>< / listcell>< / listitem> ;
< listitem>< listcell class =column2label =label2value =value2< / listcell>< / listitem>< listcell label =cell2>< / listcell> ;
< / listbox>

然而,我无法得到它显示项目的文本。正如你在上面看到的,我似乎没有正确的处理目标



我已经从这里得到了原始代码,并得到 EventListener 在这里工作

如何获取listcells的值?我已经尝试了所有的东西!

解决方案

code>在 listcell 上。最接近你可以在 listitem 上检测点击。之后,您必须使用代码进行深入分析。



因此,在您的目标上使用 .childNode 。由于在 listitem 中只有两个 listcell ,所以如果要捕获第二个单元格,可以使用的childNodes [1] 。 `childNodes [0]将引用第一个单元格。

  onLoad:function(){
var mylistbox = document .getElementById( mylistboxID);
mylistbox.addEventListener(click,function(event){
var target = event.target.childNodes [1];
if(!target){
return; / /如果没有目标

alert(target.getAttribute(label)+ target.getAttribute(label));
},false);
},


In my firefox addon I have a <listbox>. I want to be able to work a javascript function when I left-click on an item in the box. The function should retrieve the item's textual value.

Now, my function does get called when I click on a listitem, as I've placed this in my event listener's onLoad call:

    var myListBox = document.getElementById("myListBoxID");
    myListBox.addEventListener("click", function(event){
        var target = event.target;
        while (target && target.localName != "listitem"){
            target = target.parentNode;
        }
        if (!target){
            return;   // Event target isn't a list item
        }
        alert(target);                                  //returns blank
        alert(target.id);                               //returns blank
        alert(target.getAttribute("value"));        //returns blank
        alert(target.getAttribute("text"));     //returns blank
        alert(target.getAttribute("id"));           //returns blank

        var targetid = document.getElementById(target.id);
        alert(targetid);                                //returns null
    }, false);      
},

The xul goes something like this:

<listbox id="listbox1">
    <listcols /><listcol flex="1"/><listcol flex="1"/></listcols>
    <listitem><listcell class="column1" label="label1" value="value1"</listcell><listcell label="cell1"></listcell></listitem>
    <listitem><listcell class="column2" label="label2" value="value2"</listcell></listitem><listcell label="cell2"></listcell>
</listbox>

However, I can't get it to display the text of the items. As you can see above, I don't seem to have a proper handle on the target

I've gotten the original code from here, and got the EventListener working here.

How can I get the value of the listcells? I've tried everything!

解决方案

You can't detect a click on just a listcell. The closest you can go is to detect a click on a listitem. After that, you have to drill down using code.

So, use .childNode on your target. Since you have only two listcells in your listitem, if you are trying to capture the second cell, use childNodes[1]. `childNodes[0] will refer to the first cell.

onLoad: function(){
    var mylistbox= document.getElementById("mylistboxID");
    mylistbox.addEventListener("click", function(event){
        var target = event.target.childNodes[1];
        if (!target){
            return;   // In case there is not target
        }
        alert(target.getAttribute("label") + target.getAttribute("label"));
    }, false);      
},

这篇关于如何从listbox listitem中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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