开发者社区 问答 正文

如下代码如何修改,才可以达到同一个页面有多个输入框,也可以进行验证?

下面这个代码就是检测输入框有字才可以点击,缺点是只能验证1个输入框
但是我的页面排版像微博一样,同一个页面有多个输入框。求教大神怎么改才可以适用于多个输入框呢?
http://jsbin.com/gunigaxuwa/1/edit?html,css,js,output

展开
收起
云栖技术 2016-05-25 13:53:04 1769 分享 版权
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!
    <input type="text" name="title" id="title" value="title" />
    <input type="text" name="name" id="name" value="name" />
    <input type="text" name="content" id="content" value="content" />
    <input type="text" name="phone" id="phone" value="phone" />
    <button id="button">点我</button>
    
    $('#button').click(function(){
            var json = {'title':'标题必填','name':'姓名必填','content':'内容必填','phone':'电话必填'};
            for (var i in json) {
                if ($('#'+i).val() == '') {
                    console.log(json[i]);
                }
            }
        })
    2019-07-17 19:14:09
    赞同 展开评论
问答分类:
问答地址: