0

私はjqueryに少し慣れていないので、ご容赦ください。

私は登録システムに取り組んでおり、パスワードとパスワードの確認テキスト ボックスがあります。2 つのボックスの内容が変わるたびに、確認ボックスの背景色を設定したいと思います。ボックスの内容が一致するかどうかに基づいて色が決まります。

編集 - 元のコードは背景色をまったく変更していませんでした。フォーカス/ぼかしではなく、ユーザーのタイプに合わせて変更したいと思います。

私のコードは次のとおりです。

<input type="password" name="password" id="newpassword"/>
<input type = "password" name = "confirm" id="confirm"/>
<input type="submit" value="Register" id="Register"/>

<script>
    $(document).ready(function () {
        $('#password').change(function () {
            if ($('#newpassword').val().Equals($('#confirm').val())) {
                $('#confirm').attr("backgroundcolor", "green");
                $('#Register').attr("disabled", "");
            } else {
                $('#confirm').attr("backgroundcolor", "red");
                $('#Register').attr("disabled", "disabled");
            }
        });
        $('#confirm').change(function () {
            if ($('#newpassword').val().Equals($('#confirm').val())) {
                $('#confirm').attr("backgroundcolor", "green");
                $('#Register').attr("disabled", "");
            } else {
                $('#confirm').attr("backgroundcolor", "red");
                $('#Register').attr("disabled", "disabled");
            }
        })
</script>

前もって感謝します

4

3 に答える 3

2

backgroundcolor は属性ではないため、css メソッドを使用します。

$('#confirm').css("backgroundColor", "green");
于 2013-07-30T14:01:49.203 に答える
1

このコードhttp://jsfiddle.net/pQpYX/を試してください:

$(document).ready(function () {
    $('#confirm').keypress(function (event) {
        if ($('#newpassword').val() == ($('#confirm').val() + String.fromCharCode(event.keyCode))) {
            $('#confirm').css("background-color", "green");
            $('#newpassword').removeAttr("disabled");
        } else {
            $('#confirm').css("background-color", "red");
            $('#newpassword').attr("disabled", "disabled");
        }
    });
});
于 2013-07-30T14:03:32.107 に答える
1
$(document).ready(function () {
    $('#newpassword, #confirm').change(function () {
        var $n = $('#newpassword'),
            $c = $('#confirm'),
            newp = $n.val(),
            conf = $c.val();
        if (newp === conf) {
            $c.css('background-color', 'green');
            $n.prop('disabled', false)
        } else {
            $c.css('background-color', 'red');
            $n.prop('disabled', true)
        }
    });
});

これがあなたがやりたかったことであることを願っています。

フィドル

于 2013-07-30T14:12:14.240 に答える