Discussion on jsp to realize the shielded Backspace key

  • 2020-06-23 01:41:00
  • OfStack

Today I have a question. In jsp query criteria, there are text boxes to enter filter criteria and drop-down boxes to select filter criteria. Content in the input box can be deleted by pressing the "Backspace" key, but the drop-down box needs to mask the backspace function, otherwise the rollback function of the page will be executed. The following code
 
<td width="350px;"> An enterprise name :&nbsp; 
<input type="text" name="filter_psName" id="psName" size="40"/> 
</td> 
<td width="200px;"> area :&nbsp; 
<select name="filter_regionCode" id="regionName" theme="simple" /> 
</td> 
<td> 
<s:radio onclick="query();" name="filter_status" theme="simple" ></s:radio> 
</td> 
<td valign="middle" align="center"> 
<img src="${ctx}/common/img/icons/icon403a3.gif" height="20" /> The query  

</td> 

On the Internet to find 1 block block backspace key code is as follows
 
$(document).keydown(function (e) { 
var doPrevent; 
if (e.keyCode == 8) { 
var d = e.srcElement || e.target; 
if (d.tagName.toUpperCase() == 'SELECT') { 
doPrevent = d.readOnly || d.disabled; 
} 
else 
doPrevent = true; 
} 
else 
doPrevent = false; 
if (doPrevent) 
e.preventDefault(); 
}); 

The following problems were found:
The drop-down box d. tagName also gets the label name INPUT. Therefore, the above requirements cannot be realized.
Take a closer look at the 1, the following code
 
// Screen dropdown box backspace key operation  
$(document).keydown( function(e) 
{ 
// Get the keys of the keyboard CODE 
var k=e.keyCode; 
// Gets the tag object for the operation  
var obj=e.target || e.srcElement; 
// Gets the value of the read-only property of the object  
var vReadOnly = obj.getAttribute('readonly'); 
// If the button is" backspace And the read-only property of the tag object is not null return false ( select  Label the default  readonly= " readonly ").  
if(k==8 && vReadOnly!=null){ 
return false; 
} 
}); 

There should be a better way to achieve the above requirements. I hope friends can give hints or discuss with each other.

Related articles: