简体   繁体   English

输入字段的值不可编辑

[英]Value of the input field should not be editable

I have this form in my Hotel Management System. 我的酒店管理系统中有此表格。 It's the registration of Room form. 这是房间表格的注册。 So the Room Number depends on the Floor (+ 2 to 4 digits). 因此,房间号取决于楼层(+ 2到4位数字)。 Here's how it looks: 外观如下:

在此处输入图片说明

So the value that the Floor pass on the Room should not be editable. 因此,楼层在房间上传递的值不应是可编辑的。 This is the javascrpit that I use: 这是我使用的javascrpit:

<script type="text/javascript">
//<![CDATA[
$(document).ready(
        function() { 
            $("#_roomNumber_id").on("keydown", function(e) {
                   if (($(this).get(0).selectionStart == 0 && (e.keyCode < 35 || e.keyCode > 40))
                       || ($(this).get(0).selectionStart == 1 && e.keyCode == 8)) {
                       return false;
                }
               });

               $("#_roomNumber_id").bind("contextmenu", function(e) {
                   e.preventDefault();
               }); 
            }); 
//]]>
</script>

But the only thing that the javascript do is disable the characters base on the number that you declare on this line ($(this).get(0).selectionStart == 1 && e.keyCode == 8) and it shouldn't be like that, the whole value of the floor pass should not be editable. 但是javascript唯一要做的就是根据您在此行声明的数字($(this).get(0).selectionStart == 1 && e.keyCode == 8)禁用字符,并且不应那样,下车通行证的整个价值就不可编辑。 I hope someone can help me. 我希望有一个人可以帮助我。 Thank you. 谢谢。

Hope this JS Fiddle helps you to get the solution 希望这个JS小提琴能帮助您获得解决方案

<input id="field" type="text" value="Basement-" size="10" />

<div id="output">
</div>

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;
   }
});                    

You can use simple javascript to disable the select input control by using the onchange event handler! 您可以使用简单的JavaScript通过使用onchange事件处理程序来禁用选择输入控件! here goes a sample plunkr https://plnkr.co/edit/r1jR75NvI2QfzIiMeHsu?p=preview 这是一个示例plunkr https://plnkr.co/edit/r1jR75NvI2QfzIiMeHsu?p=preview

You could consider using the "Input Group" feature of bootstrap (if you're using bootstrap, that is). 您可以考虑使用引导程序的“输入组”功能(也就是说,如果使用引导程序)。 See this link: https://v4-alpha.getbootstrap.com/components/input-group/ 看到此链接: https : //v4-alpha.getbootstrap.com/components/input-group/

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

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