Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么按“后退”按钮时不删除输入文本?_Javascript_Jquery_Jquery Mobile - Fatal编程技术网

Javascript 为什么按“后退”按钮时不删除输入文本?

Javascript 为什么按“后退”按钮时不删除输入文本?,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我做了一个简单的演示,其中我限制弹出屏幕在按下后退按钮时不关闭。但我可以这样做,但当我在文本字段上写东西时,我无法删除文本。 我们能同时做这两件事吗?是否意味着限制弹出屏幕以及从文本字段中删除文本 <!DOCTYPE html> <html> <head> <link href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" rel="stylesheet" type="

我做了一个简单的演示,其中我限制弹出屏幕在按下后退按钮时不关闭。但我可以这样做,但当我在文本字段上写东西时,我无法删除文本。 我们能同时做这两件事吗?是否意味着限制弹出屏幕以及从文本字段中删除文本

<!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> 

您可以尝试创建类型为重置的隐藏输入 假设它有一个
id
clearIt

你可以这样说:

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

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

});

当输入文本位于表单上时,它将重置输入文本。

您刚刚禁用了整个文档(在您的案例正文中)的退格,包括输入。
为了不影响输入,可以检查它是否是条件中的活动元素

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

});

你的代码运行良好。但是如果我有100个不同Id的文本字段。我如何才能做到这一点。我可以添加100个文本字段吗?我可以添加任意多个Id,只要它们都是唯一的,但这似乎需要做很多工作,因此你可以更狡猾地使用jQuery,编辑上面的代码。
$(document).on('keydown', function(e) {
    if( 
        $('#myPopup').is(':visible') && 
        (!$('#myPopup input').is(':focus')) &&
        e.which === 8
    ) {
        e.preventDefault();
    }

});