JQuery移动隐藏<input>不隐藏<input>字段的周围<div>

JQuery Mobile hide <input> does not hide the surrounding <div> of the <input> field

当我在 JQuery Mobile 中使用 hide() 函数隐藏 <input> 标签时,它也隐藏了 <input> 而没有隐藏 <div> 围绕 JQuery Mobile 实施的 <input>

当然,我可以在之后处理 DOM 使其消失,但我正在寻找更好的解决方案。

情况是这样的,你会注意到 <input type="text" name="lastname" id="lastname"> 周围的 div 仍然在屏幕上(你可以 运行 它 a W3C editor like here):

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
  <h1>Text Inputs</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form method="post" action="/action_page_post.php">
      <div class="ui-field-contain">
        <label for="firstname">First name:</label>
        <input type="text" name="firstname" id="firstname">
        <label for="lastname">Last name:</label>
        <input type="text" name="lastname" id="lastname">
        <script type="text/javascript">
                    $(document).ready(
                        $('body').find('[id="lastname"]').hide()
                    );
        </script>           

      </div>
      <input type="submit" data-inline="true" value="Submit">
    </form>
  </div>
</div>

</body>
</html>

我已经尝试在 parent() 上应用 hide() 函数,但它确实把父级当作我想隐藏的 <div> 还没有在屏幕,并隐藏整个表单而不是特定字段:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
  <h1>Text Inputs</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form method="post" action="/action_page_post.php">
      <div class="ui-field-contain">
        <label for="firstname">First name:</label>
        <input type="text" name="firstname" id="firstname">
        <label for="lastname">Last name:</label>
        <input type="text" name="lastname" id="lastname">
        <script type="text/javascript">
                    $(document).ready(
                        $('body').find('[id="lastname"]').parent().hide()
                    );
        </script>           

      </div>
      <input type="submit" data-inline="true" value="Submit">
    </form>
  </div>
</div>

</body>
</html>

目前你试图访问父级 div 它没有被绘制.. 一种方法是使用 JavaScript setTimeout() 方法来隐藏 div元素:

setTimeout(function() {
  $('#lastname').parent().hide();
}, 1);
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<div data-role="page">
  <div data-role="header">
  <h1>Text Inputs</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form method="post" action="/action_page_post.php">
      <div class="ui-field-contain">
        <label for="firstname">First name:</label>
        <input type="text" name="firstname" id="firstname">
        <label for="lastname">Last name:</label>
        <input type="text" name="lastname" id="lastname">
      </div>
      <input type="submit" data-inline="true" value="Submit">
    </form>
  </div>
</div>