jQuery live check输入更改 [英] jQuery live check input changes

查看:244
本文介绍了jQuery live check输入更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个新的代码,我寻求一个聪明的单向高效解决方案。
我想完成类似于Twitter的事情,假设我们有两个
类型= Text的输入,一旦我们改变我们或者removeAttr(disabled,disables);或attr(禁用,禁用),我知道这很容易实现,当检查一个字段,但让我说,我改变了字段,下一个我改变其他字段,突然,我决定改变一个的返回,反之亦然,或两者返回到之前的值,以便提交按钮在禁用/启用之间变化。

I am stuck with a new code, and I seek a smart one-way efficient solution. I want to accomplish something similar to Twitter, Let's say we have two inputs of type=Text, and once one changes we either removeAttr("disabled", "disables"); or attr("disabled", "disabled"), I know this is easy to accomplish when checking one field, but lets say I changed on Field, and next I change the other field, and all of the sudden I decide to change one of them back, or vice versa or both back to previous values, so that the submit button changes between disabled/enabled.

这是我的jsfiddle: http://jsfiddle.net/Xu9XH /

Here's my jsfiddle: http://jsfiddle.net/Xu9XH/

<input type="text" value="Carl" name="first_name" maxlength="15" id="firstname" />
<input type="text" value="Johnson" name="last_name" maxlength="15" id="lastname" />
<br /><br />
<button id="senddata" disabled="" type="submit">Submit</button>


推荐答案

var inputs = $('input[type="text"]'),
    button = $('#senddata');

inputs.each(function() {
    $(this).data('val', this.value);
}).on('keyup', function() {
    var state = true;
    inputs.each(function(i,el) {
        if ( $(el).val() != $(el).data('val')) state = false;
    });
    button.prop('disabled', state);
});

FIDDLE

这篇关于jQuery live check输入更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆