将文本字段的一部分设为只读jquery mobile

时间:2012-09-17 21:50:55

标签: jquery-mobile

是否可以将jquery移动文本框中的部分文本设为只读?

如果以下括号是我的文本框,我想要“快乐”这个词是不可触及的

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

换句话说,人们应该能够删除所有那些时期,直到他们到达y。可能,不是吗?

谢谢!

1 个答案:

答案 0 :(得分: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>

希望这会有所帮助。