English 中文(简体)
在无主机的情况下从名单上选择和选择多种选择
原标题:Selecting and Deselecting multiple options from list without keyboard

I have the following list that is present to the user

<select id="regions" multiple="multiple">
  <option value= us-west  selected="selected">US West Coast</option>
  <option value= us-east  selected="selected">US East Coast</option>
  <option value= eur  selected="selected">Europe</option>
  <option value= asia  selected="selected">Asia</option>
</select>

如上文所述,所有项目均选定。

I was trying to implement a jquery function where if the user clicks an option on the list only the clicked item (let s say Europe) will be removed, while the rest of the options are selected. I know that this is possible by holding down "CTRL" and clicking, but is there a way where all the user has to do is to click?

我行使这一职能:

  <script type="text/javascript">
    function(){
        $("#regions options:selected").removeAttr("selected");
    };
  </script>
问题回答

因此,我只剩下一段时间,但我认为我有重新寻找的功能。

Check this fiddle

The idea is, create an array to keep track of what s selected. The natural behaviour of a multiple select box if you only click, is to deselect all other selections and select what you clicked on.

因此,我们不关心选箱中的内容,而是看到(在停用时)点击了哪一种选择,并将它列入我们的个人名单,然后更新选定箱,以反映我们名单上的内容(关于改用)。

This prevents the user from dragging to select multiple items in the selectbox, but being able to do that would probably just mess it up.

var selection = [];

$( #the_box > option ).mousedown(function(){

    if(selection.length < 1){
        selection.push($(this).val());
    } else {
        var found = 0;
        for(i = 0; i < selection.length; i++) {
            if(selection[i] == $(this).val()){
                selection.splice(i, 1);
                found = 1;
                break;
            }
        }
        if(found === 0){
            selection.push($(this).val());
        }
    }
});

$( #the_box ).bind( mouseup mouseout , function(){
    $(this).children( option ).each(function(){
        $(this).removeAttr( selected );
    });
    $(this).children( option ).each(function(){
        for(i = 0; i < selection.length; i++){
            if($(this).val() == selection[i]){
                $(this).attr( selected , selected );
            }
        }
    });
});

但是,你只能用一份清单或一套干,来做这一准确的事情,而不是使用本地的<代码><select>标签,如果你重新使用这种方法(因为不必围绕选定箱的违约行为工作),实际上会更容易这样做。

这决不是完美的解决办法,但欢迎你尝试。

To attach the function (note that this allows a callback function to be fired after selecting items .. for example if you want to populate another list based on the selection)

$( #mySelect ).click(function () {
    singleClickList(this, myCallbackFunction);
});

该职能:

function singleClickList(mythis, callback) {
    var selected = $(mythis).data( selected );
    if (selected == undefined) selected = [];
    var scrollTop = mythis.scrollTop;
    var current = $(mythis).val();
    var index;
    if (current != null) {
        if (current.length > 0) {
            index = $.inArray(current[0], selected);
            if (index >= 0)
                selected.splice(index, 1);
            else
                selected.push(current[0]);
        }
        $(mythis).val(selected);
        $(mythis).data( selected , selected);
        mythis.scrollTop = scrollTop;
        if (callback) callback();
    }
}

请注意,这要求进行分类,并利用分类法储存选定的物品。





相关问题
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.

热门标签