php - 在表单焦点上隐藏占位符仅适用于一种表单
问题描述
我正在使用一个jQuery函数来隐藏表单焦点上的占位符文本,这个函数在主页(Index.php)中的表单上完美运行,但是它不适用于使用引导程序(Members.php)设计的其他表单举个例子。我试图看看问题出在哪里,但它似乎不起作用。我不知道Bootstrap和jQuery的版本不兼容是否导致了这个问题?!我正在使用 - jquery-3.4.1.min.js - bootstrap-3.3.7
==========================Backend.js====================== ===========
// here is the jQuery function
$(function(){
'use strict';
// Hide Placeholder On Form Focus
$('[placeholder]').focus(function (){
$(this).attr('data-text',$(this).attr('placeholder'));
$(this).attr('placeholder', '');
}).blur(function (){
$(this).attr('placeholder',$(this).attr('data-text'));
});
});
===============================Index.php================= =====================
// Here is the form where the jQuery function works fine
<form class="login" action="<?php echo $_SERVER['PHP_SELF']?>" method = "POST">
<h4 class="text-center">Admin Login</h4>
<input class = "form-control input-lg" type="text" name="user" placeholder="Username" autocomplete="off"/>
<input class = "form-control input-lg" type="password" name="pass" placeholder="Password" autocomplete="new-password"/>
<input class = "btn btn-lg btn-primary btn-block" type="submit" value="Login" />
</form>
================================Members.php================ ====================
// Function doesn't work on this form
<div class="container">
<form class="form-horizontal" action="?do=Insert" method="POST">
<!-- start username field -->
<div class="form-group">
<label class="col-sm-2 control-label" >Username</label>
<div class="col-sm-8">
<input type="text" name="username" class="form-control" autocomplete="off" required = "required" placeholder="Enter User Name" />
</div>
</div>
</form>
</div>
解决方案
var input = $("input");
input.focus(function(){
$(this).attr("placeholder", " ")
});
input.blur(function(){
$(this).attr("placeholder", "example")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="example">
推荐阅读
- javascript - node js 完整的 javascript 对象未显示在终端和控制台中
- reactjs - 如何在 React-Bootstrap 中将地图循环内的状态变量访问显示为水平选项卡?
- typescript - 使用 webpack 和 babel-loader 进行编译时,TypeGraphQL @Arg 装饰器失败并出现解析器错误
- reactjs - 如何在使用地图功能时更新一个特定元素的状态
- authentication - React Router:在渲染私有路由之前检查 JWT 是否有效
- adonis.js - 有两个带有信息的 serperate const 并将它们匹配起来
- javascript - 根据 Firestore 中的 id 更新单个文档
- php - 如果另一个表中存在重复项,则更改自动增量值
- python - 如何使用 lxml 正确格式化 xml 文件?
- java - 如何将 Guardian API 添加到依赖项