简体   繁体   English

设置无法删除的默认文本框值

[英]Set default textbox value that can't be removed

All values for a text box begin with T- 文本框的所有值都以T-开头

how do I have a textbox prepopulated with T- where the user cannot remove the T- 我如何预先填充T-文本框,用户无法删除T-

Thanks for your help 谢谢你的帮助

Try this 尝试这个

<input id="field" type="text" value="T-" size="50" />

JS JS

var readOnlyLength = $('#field').val().length;

 $('#output').text(readOnlyLength);

$('#field').on('keypress, keydown', function(event) {
    var $field = $(this);
    $('#output').text(event.which + '-' + this.selectionStart);
    if ((event.which != 37 && (event.which != 39))
        && ((this.selectionStart < readOnlyLength)
        || ((this.selectionStart == readOnlyLength) && (event.which == 8)))) {
        return false;
    }
});  

DEMO DEMO

Attach the label before the textbox and give textbox left-border to 0px and label right-border to 0px. 在文本框之前附加标签,并将文本框左边框设置为0px,将右边框标记为0px。 While getting the value add both the values. 获取值时,同时添加两个值。 hope it helps you. 希望它能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM