首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有空值的表单输入

带有空值的表单输入
EN

Stack Overflow用户
提问于 2015-03-26 07:45:17
回答 2查看 52关注 0票数 0

当我试图识别带有空值的输入时,或者在没有="“的情况下只获得值属性时,我遇到了问题。请看下面我的代码。

代码语言:javascript
复制
<form id='form'>
<div class='input-field col s12'>
    <input id='title' type='text' value>
    <label for='title'>Job Tilte</label>
</div>
<div class='input-field col s12'>
    <input type='text' id='company' value='test' />
    <label for='company'>Company Name</label>
</div>
<div class='input-field col s12'>
    <input type='text' id='telephone' value='' />
    <label for='telephone'>Telephone Number</label>
</div>
<div class='input-field col s12'>
    <textarea id='description' placeholder='test2'></textarea>
    <label for='description'>About you</label>
</div>
<div class='input-field col s12'>
    <input type='password' id='password' />
    <label for='password'>Your password</label>
</div>
<div class='input-field col s12'>
    <input type='password' id='re-password' />
    <label for='re-password'>Confirm your password</label>
</div>
<input type='submit' disabled='disabled' value='Update' id='sendbutton' />

这是我的jQuery:

代码语言:javascript
复制
if ($('#form input').val().length !== 0 ) {
        $(this).hide();
        } else {
        $(this).next().addClass('active');
        }

有人能帮我解决这个问题吗?在此之前,非常感谢您。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-26 07:50:25

使用下面的代码。每个() --您可以在#表单中获取所有输入元素。请参阅演示

代码语言:javascript
复制
  $(document).ready(function(){
   $('#form input').each(function(){
     if ($(this).val().length !== 0 ) {
        $(this).hide();
     } else {
       $(this).next().addClass('active');
     }
  });
 });
票数 1
EN

Stack Overflow用户

发布于 2015-03-26 07:51:15

你需要

代码语言:javascript
复制
$('#form input').each(function () {
    if (this.value.length !== 0) {
        $(this).hide();
    } else {
        $(this).next().addClass('active');
    }
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29273262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档