我编写了这个简单的 js 来将类添加到焦点输入中,并在失去焦点时将其删除(如果值为空)。但是,无论该值是否为空,该类都不会被删除。感谢您的帮助,非常感谢
HTML:
<form id="prospects_form" method="post" action="...">
<input id="form_name" type="text" name="name" placeholder="Name*" />
<input id="form_email" type="text" name="email" placeholder="Email*" />
<input id="form_subject" type="text" name="subject" placeholder="Subject*" maxlength="50" />
<textarea id="form_message" rows="6" cols="5" name="message" placeholder="Message*" maxlength="500"></textarea>
<button id="form_send" class="btn btn_white" type="submit">Send</button>
</form>
JS:
// When input is focussed
$('#prospects_form > *').focus(function() {
$(this).addClass("hasText");
});
$('#prospects_form > *').blur(function() {
if ($(this).val === '') { $(this).removeClass("hasText"); }
});