简体   繁体   English

带有jQuery的html5占位符-javascript输入值请参见占位符

[英]html5 placeholder with jquery - javascript input value see placeholder

I have a page that another part of our team uses jquery for some functions. 我有一个页面,我们团队的另一部分使用jquery来实现某些功能。 When the jquery* files are called my javascript validation see the HTML5 Placeholders as value. 当jquery *文件被称为我的JavaScript验证时,请参见HTML5占位符作为值。 If I take out the jquery everthign works fine. 如果我拿出jQuery,everthign可以正常工作。

For example if I have this: 例如,如果我有这个:

!document.getElementById("company").value

With the jquery in the html file either in the header or in the body my JS does not see this as null or empty it see is with a value of the HTML5 Placeholder. 在标题或正文中的html文件中使用jquery时,我的JS不会将其视为null或为空,而看到的是带有HTML5占位符的值。

<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/jquery.main.js"></script>

Thank you for any help you can give. 感谢您提供的任何帮助。

If I understood you correctly, you have a form with a placeholder attribute which works fine. 如果我对您的理解正确,那么您的表单具有placeholder属性,可以很好地工作。 In your code, using vanilla javascript, you're reading out the value attribute of fields of that form and it normally returns an empty string if that field is empty. 在您的代码中,使用香草javascript,您正在读取该形式的字段的value属性,如果该字段为空,则通常会返回一个空字符串。 But now, upon including jQuery, asking for the value attribute returns the placeholder text instead. 但是现在,在包含jQuery时,要求value属性返回placeholder文本。

It sounds like a bug to me. 对我来说,这听起来像个虫子。 Are you by any chance using IE? 您是否有机会使用IE? This is not a solution, but a work around. 这不是解决方案,而是一种解决方法。 What you can do is compare the value you get to the placeholder text. 您可以做的是将您获得的值与占位符文本进行比较。 If it's equal, then you conclude that the field is still empty: 如果相等,则可以得出该字段仍然为空的结论:

<input name="company" type="text" id="company" onFocus="changebrc('Company');" placeholder="Company Name"/>

if (document.getElementById("company").value == "Company Name"){/*field is empty*/}

This will not work if the user decides to enter the same text written in the placeholder, which is quite unlikely if proper placeholder text is used. 如果用户决定输入在占位符中写入的相同文本,则这将不起作用,如果使用正确的占位符文本,则不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM