javascript 如何仅将文本框中的某些文本设为只读,同时允许编辑其余文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15181417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make only some text in a text-box read-only while allowing the rest to be edited
提问by Ryan Taylor
I'm trying to use Javascript to make a text box that contains some read-only text at the beginning of the text box and then allows editing following the read-only text. How can I do this in Javascript/jquery?
我正在尝试使用 Javascript 制作一个文本框,该文本框在文本框的开头包含一些只读文本,然后允许在只读文本之后进行编辑。我怎样才能在 Javascript/jquery 中做到这一点?
回答by John S
Here's an attempt:
这是一个尝试:
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;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input id="field" type="text" value="CAN'T TOUCH THIS!" size="50" />
<div id="output">
</div>
This disables keys other than the left and right arrows for the read-only part. It also disables the backspace key when just at the end of the read-only part.
这将禁用只读部分的左右箭头以外的键。它还会在只读部分的末尾禁用退格键。
From what I've read, this won't work on IE <= 8.
根据我的阅读,这不适用于 IE <= 8。
Here it is in plain JavaScript (no JQuery):
这是纯 JavaScript(没有 JQuery):
function makeInitialTextReadOnly(input) {
var readOnlyLength = input.value.length;
field.addEventListener('keydown', function(event) {
var which = event.which;
if (((which == 8) && (input.selectionStart <= readOnlyLength)) ||
((which == 46) && (input.selectionStart < readOnlyLength))) {
event.preventDefault();
}
});
field.addEventListener('keypress', function(event) {
var which = event.which;
if ((event.which != 0) && (input.selectionStart < readOnlyLength)) {
event.preventDefault();
}
});
}
makeInitialTextReadOnly(document.getElementById('field'));
<input id="field" type="text" value="CAN'T TOUCH THIS!" size="50" />
回答by Hussein Nazzal
here is some thoughts
这是一些想法
roughly a solution with jquery
粗略的用jquery解决
html
html
<input type="text" id='myinput' value ="my text">
jquery
查询
var orginal_text = $('#myinput').val();
var regular_expression = '/^' + orginal_text +'/' ;
$('#myinput').keyup(function(){
var current_text = $('#myinput').val();
if(current_text.match('^' + orginal_text +'') == null){
$('#myinput').val(orginal_text + ' ' +current_text )
}
})
with css
用CSS
html
html
<input type="text" id='my_sticky_text' value ="my text" readonly='readonly'>
<input type="text" id='myinput' value ="my text">
css
css
#my_sticky_text{
position:absolute;
left : 0px;
}
#myinput{
position:absolute;
left : 50px;
border-left:none;
}
combination to retrieve the value
组合检索值
html
html
<input type="text" id='my_sticky_text' value ="my text" readonly='readonly'>
<input type="text" id='myinput' value ="my text">
<br>
<hr>
<button id='getval'>get value</button>
css
css
#my_sticky_text{
position:absolute;
left : 0px;
}
#myinput{
position:absolute;
left : 50px;
border-left:none;
}
jquery
查询
$('#getval').click(function(){
var sticky_text = $('#my_sticky_text').val();
var user_text = $('#myinput').val();
alert (sticky_text + ' ' + user_text)
})
what do we get from all this ?!.. simply you cant acomplish what you want in a nice way .. and i cant imagine a situation where i want to do so .
我们从这一切中得到了什么?!.. 只是你无法以一种好的方式完成你想要的......我无法想象我想要这样做的情况。
alternatives
备择方案
1 - in the text-field label put the constant text .
1 - 在文本字段标签中放置常量 text 。
2 - when the user submits the form capture the value of the text-field and add your text to it .
2 - 当用户提交表单时,捕获文本字段的值并将您的文本添加到其中。
3- add a help note to the user that this input should be as follows (eg : mytext-yourtext)
3- 向用户添加帮助说明,该输入应如下所示(例如:mytext-yourtext)
回答by Ryan Cogswell
Below is a modified version of John S's answer to prevent cut/paste operations (e.g. via right-click):
以下是 John S 的答案的修改版本,以防止剪切/粘贴操作(例如,通过右键单击):
function makeInitialTextReadOnly(input) {
var readOnlyLength = input.value.length;
input.addEventListener('keydown', function(event) {
var which = event.which;
if (((which == 8) && (input.selectionStart <= readOnlyLength))
|| ((which == 46) && (input.selectionStart < readOnlyLength))) {
event.preventDefault();
}
});
input.addEventListener('keypress', function(event) {
var which = event.which;
if ((event.which != 0) && (input.selectionStart < readOnlyLength)) {
event.preventDefault();
}
});
input.addEventListener('cut', function(event) {
if (input.selectionStart < readOnlyLength) {
event.preventDefault();
}
});
input.addEventListener('paste', function(event) {
if (input.selectionStart < readOnlyLength) {
event.preventDefault();
}
});
}
makeInitialTextReadOnly(document.getElementById('field'));
Fiddle: http://jsfiddle.net/p0jyktvu/3/