在 YUI 3 中,当不选择任何内容时,需要刷新 Javascript 选择框吗?

发布于 2024-07-29 18:37:41 字数 936 浏览 1 评论 0原文

我使用 YUI 3 让某人单击“全选”或“全选”,然后让选择框分别选择所有项目或取消选择所有项目。 这是我的代码:

// This selects all
    Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.removeAttribute('selected');
               this.setAttribute('selected','selected');
            });
        }, selectAllNode
    );

// This selects none
   Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.setAttribute('selected','false');
               this.removeAttribute('selected');
            });
            selectBoxNode.('selectedIndex',-1);
        }, selectNoneNode
    );

selectAllLink、selectNoneLink 和 selectBoxNode 是不言而喻的、正确返回的节点。 更新: selectAll 有效,我必须手动删除每个属性并重新添加它。

selectNoneLink 不起作用:它仅取消选择之前未选择的元素...尽管 DOM 检查显示 selectedIndex 属性确实更改为 -1,所以也许它需要刷新?

任何帮助,将不胜感激。 如果这种情况发生在所有框架中,那也很高兴知道。

谢谢!

I'm using YUI 3 to let someone click "Select All" or "Select None" and then have the selectbox select all the items or unselect all of them, respectively. Here's my code:

// This selects all
    Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.removeAttribute('selected');
               this.setAttribute('selected','selected');
            });
        }, selectAllNode
    );

// This selects none
   Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.setAttribute('selected','false');
               this.removeAttribute('selected');
            });
            selectBoxNode.('selectedIndex',-1);
        }, selectNoneNode
    );

selectAllLink, selectNoneLink, and selectBoxNode are self-evident, properly returned Nodes. Update: selectAll works, I had to manually remove the 'selected' attribute for each and re-add it.

The selectNoneLink doesn't work: it unselects only the elements that weren't before selected... although DOM inspection shows that the selectedIndex attribute is indeed changed to -1, so maybe it needs a refresh?

Any help would be appreciated. If this happens in all frameworks, that would be nice to know as well.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-08-05 18:37:41

这对我有用。

YUI().use('node', function(Y) {
   Y.get('#unsel').on('click', function(e) {
     Y.get('select').get('options').set('selected',false);
   });
   Y.get('#sel').on('click', function(e) {
     Y.get('select').get('options').set('selected', true );
   });
});

This worked for me.

YUI().use('node', function(Y) {
   Y.get('#unsel').on('click', function(e) {
     Y.get('select').get('options').set('selected',false);
   });
   Y.get('#sel').on('click', function(e) {
     Y.get('select').get('options').set('selected', true );
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文