输入未解决的任务

时间:2017-01-01 23:22:49

标签: javascript jquery html css input

我有这段代码:

http://jsfiddle.net/4Qsa8/111/

如您所见,当您键入字母时,此输入的大小会增加。但问题是,当我从末尾删除第一个字母时,输入不会减少,当我从末尾删除第二个字母时,它会开始减少。所以问题是如何在删除最后一个字母之前减小输入的大小,而不是在第二个字母上,在感谢之前。

-HTML

<div class="resizing-input">
    * <input type="text" placeholder="placeholder"/>
    <span  style="display:none"></span>
</div>

-CSS

.resizing-input input, .resizing-input span {
    font-size: 20px;
    font-family: Sans-serif;
    white-space: pre;
    padding: 1px;
}

-JS

$(document).ready(function () {
    var $inputs = $('.resizing-input');

    // Resize based on text if text.length > 0
    // Otherwise resize based on the placeholder
    function resizeForText(text) {
        var $this = $(this);
        if (!text.trim()) {
            text = $this.attr('placeholder').trim();
        }
        var $span = $this.parent().find('span');
        $span.text(text);
        var $inputSize = $span.width();
        $this.css("width", $inputSize);
    }

    $inputs.find('input').keypress(function (e) {
        if (e.which && e.charCode) {
            var c = String.fromCharCode(e.keyCode | e.charCode);
            var $this = $(this);
            resizeForText.call($this, $this.val() + c);
        }
    });

    // Backspace event only fires for keyup
    $inputs.find('input').keydown(function (e) { 
        if (e.keyCode === 8 || e.keyCode === 46) {
            resizeForText.call($(this), $(this).val());
        }
    });

    $inputs.find('input').each(function () {
        var $this = $(this);
        resizeForText.call($this, $this.val())
    });
});

2 个答案:

答案 0 :(得分:2)

使用np.hstack([filenames[:, None], predictions]) #array([['file1', '0.2', '0.9'], # ['file2', '0.01', '0.0'], # ['file3', '0.3', '0.8']], # dtype='|S32') 事件代替keyup事件。因为keydown事件发生在输入更新其值之前。它会起作用。

答案 1 :(得分:0)

jsfiddle示例的第26行,更改行:

$inputs.find('input').keydown(function (e) { 

为:

$inputs.find('input').keyup(function (e) {