[英]Javascript / jQuery: How to prevent active input field from being changed?
how do I prevent the user to change the value in an input field (which contains a certain value to be copied into the clipboard) without using disabled="true"? 如何在不使用disabled =“true”的情况下阻止用户更改输入字段中的值(包含要复制到剪贴板的特定值)? The text should be selected once the user clicks in the field (that's working already) but entering anything should have no effect. 一旦用户点击该字段(已经有效),就应该选择该文本,但输入任何内容都应该没有效果。
Thanks 谢谢
jQuery('input.autoselect[value]').focus(function() { jQuery(this).select(); });
只需在输入的html中读取即可阻止用户编辑输入。
<input readonly="readonly" type="text" value="You can't edit me!"/>
I think this should work: 我认为这应该有效:
var val = jQuery('input.autoselect[value]').val();
jQuery('input.autoselect[value]').change( function(){
jQuery(this).val(val);
});
Or possibly (not sure) 或者可能(不确定)
jQuery('input.autoselect[value]').keydown(function(){
return false;
});
Based on Pim's answer , how about this? 根据Pim的回答 ,这个怎么样?
jQuery(document).ready(function(){
jQuery("input.readonly").each(function(){
jQuery(this).attr("savedVal", jQuery(this).val());
});
jQuery("input.readonly").change(function(){
jQuery(this).val(jQuery(this).attr("savedVal"));
});
});
(completely untested, but it demonstrates the idea...) (完全未经测试,但它证明了这个想法...)
<script>
document.getElementById("newval").readOnly = true;
</script>
Watch for the capital "O" in readonly... 在readonly中注意首都“O”......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.