【问题标题】:How to remove the default text "Please fill out this field"如何删除默认文本“请填写此字段”
【发布时间】:2016-09-23 10:04:16
【问题描述】:

我有一个输入字段

我需要删除默认文本请填写此字段 我使用 jQuery 函数将标题设置为空

$('#Form input[type=text]').each(function () {
    $(this).attr('title', ' ');
});

除了 IE 和 Safari 都可以正常使用。

然后我尝试了 novalidate 但这仅在输入字段位于表单内时才有效

这是我的示例代码

    <head runat="server">
    <title></title>
    <style>


.div2 .grp {
    position: relative;
    margin: 0 2%;
    margin-bottom: 31px !important;
    line-height: normal !important;
}

.div2 input {
    font-size: 14px;
    padding: 6px 10px 6px 0 !important;
    display: block;
    width: 100%;
    border: none;
    border-bottom: 1px solid #757575;
    background: transparent!important;
}

.div2 label {
    font-size: 14px;
    font-weight: normal;
    position: absolute;
    pointer-events: none;
    left: 0;
    top: 10px;
    transition: .2s ease all;
    -moz-transition: .2s ease all;
    -webkit-transition: .2s ease all;
    color:Black;
}

.div1 {
    width: 14%;
    padding: 1%;
    float: left;
   }


label {
    display: inline-block;
    max-width: 100%;
    font-weight: bold;
}

</style>
</head>
<body>
    <form id="form1" runat="server">

       <div class="div1" >
            <div class='div2'>
                <div class="grp" id="divFirstName">      
                <input type="text" runat="server" id="TB_TestForm" onclick="TestForm()"  onblur="TestForm()" required/>
                <label>My first name<span class="reqstar">*</span></label>
                <span class="required"></span>
            </div>

       </div>
    </form>
</body>
</html>
<script>
    function TestForm() {
        var html = document.getElementById("divFirstName").style.position;
        if (html == "relative" || html=="") {
            document.getElementById("divFirstName").style.position = "inherit";
        }
        else {
            document.getElementById("divFirstName").style.position = "relative";
        }
    }
</script>

请帮忙

【问题讨论】:

  • 显示输入字段的 HTML。我假设您使用的是 placeholder 属性?
  • 请向我们展示您的 html 标记,同时确保您将输入设置为空字符串 "" 而不是空格 " " 就像您在上面所做的那样
  • It works fine except IE and Safari. 那么浏览器缓存呢?!...无论如何,你应该使用$('#Form input[type=text][title]').removeAttr('title');
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现所需的最短代码在问题本身。请阅读:minimal reproducible example
  • 我不能使用占位符或标题属性,我的输入字段是这样的

标签: javascript jquery google-chrome internet-explorer


【解决方案1】:

如果您的 HTML 元素使用 placeholder 属性,您可以使用以下代码(不需要原生 JS jQuery):

document.getElementById('input').placeholder = '';
&lt;input id="input" type="text" name="fname" placeholder="First name"&gt;

注意:请提供您的 HTML,以便我们为您制定临时解决方案。

【讨论】:

  • 这肯定是错误的,因为 OP 说它适用于浏览器 except IE and Safari...
  • @Ted 因此开头的“如果”...(我同意你的观点,需要先澄清)
  • @freedomn-m - 应该是评论
  • @GibboK 我已经尝试过这段代码.. 但它在我的代码中不起作用。我的输入字段有一些样式功能。如果我使用占位符或标题属性,我的样式将无法正常工作。所以我使用简单的 html
  • @Roshmi 请发布您的 html :)
猜你喜欢
  • 1970-01-01
  • 2018-11-20
  • 2017-04-23
  • 2012-06-03
  • 2019-01-04
  • 2011-02-21
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
相关资源
最近更新 更多