简体   繁体   English

按返回按钮时为什么输入文字没有被删除?

[英]why input text not deleted when press back button?

I make a simple demo in which I restrict pop up screen not to close when back button press .But I am able to do that but when I write something on text field I am not able to delete text. 我做了一个简单的演示,其中我限制弹出屏幕不要在后退按钮按下时关闭。但是我能够这样做但是当我在文本字段上写东西时我无法删除文本。 can we do both thing ? 我们可以做两件事吗? mean restrict pop up screen as well as delete text from the text field ? 意味着限制弹出屏幕以及从文本字段中删除文本?

<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>

</head>
<body>
  <div data-role="page">
  <div data-role="header">
    <h1>Welcome To My Homepage</h1>
  </div>

  <div data-role="main" class="ui-content">
    <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all">Show Popup</a>

    <div data-role="popup" id="myPopup" data-dismissible='false'>

    <div data-role="fieldcontain">
        <label for="testCaseIDValue">TestCase Name:</label>
        <input type="text" name="testCaseIDValue" id="testCaseInnerIDValue" value="" class="inputTextTestCase"/>
    </div>
    <a href="#" data-role="button" id="doneInnerPopUp" class="common-button">Done</a>
  </div>

  <div data-role="footer">
    <h1>Footer Text</h1>
  </div>
</div> 

</body>
</html> 

js Code js代码

$('body').keydown(function(e) {

    if($('#myPopup').is(':visible')) {
        if(e.keyCode == 8) { // 8 is backspace
            e.preventDefault();
        }
    }

});

you can try to create hidden input of type reset lets say it has an id of clearIt 你可以尝试创建类型reset隐藏input ,让我们说它的idclearIt

and you can say like : 你可以说:

$('body').keydown(function(e) {

    if($('#myPopup').is(':visible')) {
        if(e.keyCode == 8) { // 8 is backspace
           $('#clearIt').trigger("click");       
 }
    }

});

it will reset the input text when it is on a form 当它在表单上时 ,它将重置输入文本

You've just disabled the backspace for the entire document (in your case body), including the input. 您刚刚为整个文档(在您的案例正文中)禁用了退格键,包括输入。
To not affect the input, you can check if it is the active element in the condition 要不影响输入,可以检查它是否是条件中的活动元素

$(document).on('keydown', function(e) {
    if( 
        $('#myPopup').is(':visible') && 
        (!$('#myPopup input').is(':focus')) &&
        e.which === 8
    ) {
        e.preventDefault();
    }

});

FIDDLE 小提琴

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

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