English 中文(简体)
IE javascript: 当选择被点击时,从选定的多个选择中删除选择不适用。
原标题:IE javascript: Remove option node from a select multiple when option is clicked

I have a code that removes an option element from a select multiple(<select multiple></select>) tag when the option is clicked. The code works +- like this.

var option = new Option("sometext","someval");
$(option).click( function(){
                selectBox.remove(option);
                return false;
            }
        );
$(selectBox).append(option);

除了IE之外,它还在我能够记住的所有发动机(Gecko, presto, webkit...)上运行良好。

According to my research, IE does not support adding events to option tags so I added the event to the select tag.

$(selectBox).click( function(event){
                if (option === selectBox.options[selectBox.selectedIndex]) {
                    event.preventDefault();
                    event.stopPropagation();
                    selectBox.remove(option);
                    return false;
                }
                event.preventDefault();
                return true;
            }
        );

Where is: if (option === selectBox.options[selectBox.selectedIndex]) {
I already tried: if ($(option).is( :selected )) {
Didn t work either.

What happens: When I click an option all binded functions are activated and there can be activated functions after the element is deleted.
Using event.stopPropagation(); (propagation does continue, anyway) seem not to be doing what I want as all other events are also called. Changing the event from click to change does not solve the problem because when the selected option is changed it is deleted and then the selected option becomes another one causing the event to be called again and, in the end, all elements in the select are deleted.

最佳回答

Try this Code; Assume selectBox to be a DOM content, a J Query Object, or a CSS selectedor which points to the <select> elements:

$(selectBox).click(function(){
    var selectedOption = this.selectedIndex;
    $("option", selectBox).each(function(index){
        if(index == selectedOption){
            $(this).remove();
            return false; //Stop looping
        }
    });
})

选定的选择通过<代码>选定内容的Index/code>财产。 如果每个选择都通过,则选定选择——在某一点找到。 在发现这一要素时,该办法被删除,而休息时间则使用<代码>return false。

我建议使用<条码>,而不是使用<条码>。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签