jQuery val(); input 获取与设置默认输入内容

137 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="jquery-3.4.1.js"></script>
  <script>
    $(document).ready(function () {
      // 插入默认值
      $('button').eq(0).click(function () {
        $('input').val("dzmdzmdzm");
      })
      // 获取
      $('button').eq(1).click(function () {
        var str = $('input').val();
        $('span').html(str)
      })
    })
  </script>
</head>
<body>
  <button>插入默认字符串</button>
  <input class="txt" type="text" placeholder="请输入">
  <button>获取</button>
  <span></span>
</body>
</html>
  • demo 效果: