0

是否可以在 jquery 移动文本框中有一部分文本是只读的?

如果以下括号是我的文本框,我希望“快乐”这个词是不可触碰的

[happy...................]

换句话说,人们应该能够删除所有这些期间,直到他们到达 y。有没有可能?

谢谢!

4

1 回答 1

0

我想你可能想尝试这样的事情:

JS/jQuery 代码:

// Your untouchable string
var my_string = "happy";

$(function () {

    // The following function makes sure that when the user is typing, //
    // the user won't touch the word "happy"
    $("#my_input").keyup(function () {
        var string = $(this).val();

        if(string.length >= my_string.length) {
            if(string.substring(0, my_string.length) != my_string) {
                // This part makes sure that the user won't modify the word "happy"
                $(this).val(my_string+string.substring(my_string.length+1, string.length));
            }
        } else {
            // This part makes sure that the user won't typed:
            //   - before the word "happy" (eg: "Zhappy")
            //   - inside the word "happy" (eg: "hapZpy")
            $(this).val(my_string);
        }
    });
});

HTML(示例):

<body>
    <div data-role="page">
        <div data-role="content">

            <!-- The value of your input is initially set to your untouchable string -->
            <input id="my_input" type="text" value="happy"/>
        </div>
    </div>
</body>

希望这可以帮助。

于 2012-09-18T02:26:05.340 回答