Initialization Error

In my input element, when I go to the element and enter the backspace key using the keyboard, it goes to the previous page when I set the readonly input.

My code is below. Please share your knowledge.

 <div class="div1"> <label class="div1" for="inputfor">cash:</label> <input type="text" id="cashinput" readonly="readonly" /> </div> 
+8
source share
5 answers

This is the expected and correct behavior for input with the readonly attribute.

Input with this attribute cannot be changed, it can only be viewed, you can access the content by tabbing and copying, but that’s all.

Remove the readonly attribute if it is not needed.

0
source

I know this question was asked two years ago. Since I have a solution that worked for me, I am tempted to share it with everyone.

The fix is ​​pretty simple:

 <input type="text" onkeydown="event.preventDefault()" readonly="readonly"/> 

Event.preventDefault () will stop the backspace from moving from the page, and you can also select and copy text.

Thanks.

+31
source

You can always just leave the input field for reading only completely disabled.

 <div class="div1"> <label class="div1" for="inputfor">cash:</label> <input type="text" id="cashinput" onkeydown="return false;" readonly="readonly"/> </div>​​​​​​​​​​​​​​​ 
+4
source

This line helped me:

 onkeydown="if(this.readOnly) event.preventDefault();" 
0
source

if you are on the page because of location.href = "new / page / url";

use location.replace ("new / page / url").

location.href = "new / page / url"; // load a new page and save the current page as history in IE

see https://developer.mozilla.org/en-US/docs/Web/API/Location

-2
source

All Articles