【问题标题】:Jquery Selectbox I beam IssueJquery Selectbox I Beam 问题
【发布时间】:2013-04-03 08:19:52
【问题描述】:

这是一个选择框(我已经使用并修改过)。当我单击选择框时,它会变成文本输入并显示工字梁。只是想禁用这个工字梁。我怎样才能得到它?

Check JSfiddle

这里是html:

<select name="Items" id="Items">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>    
</select>

和 Jquery:

$(document).ready(function () {
    $('#Items').selectbox();
});

jQuery.fn.extend({
    selectbox: function (options) {
        return this.each(function () {
            new jQuery.SelectBox(this, options);
        });
    }
});


if (!window.console) {
    var console = {
        log: function (msg) {}
    }
}

jQuery.SelectBox = function (selectobj, options) {

    var opt = options || {};
    opt.inputClass = opt.inputClass || "selectbox";
    opt.containerClass = opt.containerClass || "selectbox-wrapper";
    opt.hoverClass = opt.hoverClass || "current";
    opt.currentClass = opt.selectedClass || "selected"
    opt.debug = opt.debug || false;

    var elm_id = selectobj.id;
    var active = -1;
    var inFocus = false;
    var hasfocus = 0;
    //jquery object for select element
    var $select = $(selectobj);
    // jquery container object
    var $container = setupContainer(opt);
    //jquery input object 
    var $input = setupInput(opt);
    // hide select and append newly created elements
    $select.hide().before($input).before($container);


    init();

    $input.click(function () {
        if (!inFocus) {
            $container.toggle();
        }
    })
        .focus(function () {
        if ($container.not(':visible')) {
            inFocus = true;
            $container.show();
        }
    })
        .keydown(function (event) {
        switch (event.keyCode) {
            case 38:
                // up
                event.preventDefault();
                moveSelect(-1);
                break;
            case 40:
                // down
                event.preventDefault();
                moveSelect(1);
                break;

            case 13:
                event.preventDefault();
                $('li.' + opt.hoverClass).trigger('click');
                break;
            case 27:
                hideMe();
                break;
        }
    })
        .blur(function () {
        if ($container.is(':visible') && hasfocus > 0) {
            if (opt.debug) console.log('container visible and has focus')
        } else {
            hideMe();
        }
    });


    function hideMe() {
        hasfocus = 0;
        $container.hide();
    }

    function init() {
        $container.append(getSelectOptions($input.attr('id'))).hide();
        var width = $input.css('width');
        $container.width(width);
    }

    function setupContainer(options) {
        var container = document.createElement("div");
        $container = $(container);
        $container.attr('id', elm_id + '_container');
        $container.addClass(options.containerClass);

        return $container;
    }

    function setupInput(options) {
        var input = document.createElement("input");
        var $input = $(input);
        $input.attr("id", elm_id + "_input");
        $input.attr("type", "text");
        $input.addClass(options.inputClass);
        $input.attr("autocomplete", "off");
        $input.attr("readonly", "readonly");
        $input.attr("tabIndex", $select.attr("tabindex"));

        return $input;
    }

    function moveSelect(step) {
        var lis = $("li", $container);
        if (!lis) return;

        active += step;

        if (active < 0) {
            active = 0;
        } else if (active >= lis.size()) {
            active = lis.size() - 1;
        }

        lis.removeClass(opt.hoverClass);

        $(lis[active]).addClass(opt.hoverClass);
    }

    function setCurrent() {
        var li = $("li." + opt.currentClass, $container).get(0);
        var ar = ('' + li.id).split('_');
        var el = ar[ar.length - 1];
        $select.val(el);
        $input.val($(li).html());
        return true;
    }

    function getCurrentSelected() {
        return $select.val();
    }

    function getCurrentValue() {
        return $input.val();
    }

    function getSelectOptions(parentid) {
        var select_options = new Array();
        var ul = document.createElement('ul');
        $select.children('option').each(function () {
            var li = document.createElement('li');
            li.setAttribute('id', parentid + '_' + $(this).val());
            li.innerHTML = $(this).html();
            if ($(this).is(':selected')) {
                $input.val($(this).html());
                $(li).addClass(opt.currentClass);
            }
            ul.appendChild(li);
            $(li)
                .mouseover(function (event) {
                hasfocus = 1;
                if (opt.debug) console.log('over on : ' + this.id);
                jQuery(event.target, $container).addClass(opt.hoverClass);
            })
                .mouseout(function (event) {
                hasfocus = -1;
                if (opt.debug) console.log('out on : ' + this.id);
                jQuery(event.target, $container).removeClass(opt.hoverClass);
            })
                .click(function (event) {
                var fl = $('li.' + opt.hoverClass, $container).get(0);
                if (opt.debug) console.log('click on :' + this.id);
                $('li.' + opt.currentClass).removeClass(opt.currentClass);
                $(this).addClass(opt.currentClass);
                setCurrent();
                hideMe();
            });
        });
        return ul;
    }

};

谢谢。

【问题讨论】:

  • 在小提琴中,我既没有看到文本输入框,也没有看到工字梁。
  • @Beetroot-Beetroot- 关注黑色区域(在文本选项 1 后有一个白色工字梁,像这样 >> 选项 1| 。这仅在您单击它并且当下拉菜单显示)
  • 哦等等,在 Firefox 中我看到了这条线,但在 chrome 中它没问题
  • yup on firefox 需要提及。
  • 别担心,我现在只是进一步检查一下。它在 jquery 中的东西

标签: jquery html drop-down-menu jquery-selectbox


【解决方案1】:

我假设您尝试使用 this jquery plugin

之所以有“线”(又名文本框光标)是因为这个功能:

function setupInput(options) {
    ...
    $input.attr("type", "text");
    ...
}

返回一个文本框。

不完全确定你想要实现什么,但由于插件文档不需要这样的功能,那么你应该尝试在不使用&lt;input type="text"/&gt;的情况下实现它,也许只返回一个&lt;div/&gt;并通过jQuery设置它的值?

【讨论】:

  • 是的,这就是插件,非常感谢。刚刚改成这个:$input.attr("type", "button"); 现在它工作正常(I-beam 消失了,因为它不再是文本输入)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多