如何使用变量进行jquery验证equalTo方法?

时间:2014-05-10 11:13:37

标签: javascript jquery validation jquery-selectors jquery-validate

我想将变量用于jquery验证equalTo方法。但不适用于jquery选择器。我怎么能这样做?

HTML

<form id="form" method="post">
    <div>
        <input type="password" name="pass1" id="passM" required />
    </div>
    <div>
        <input type="password" name="pass2" id="passR" required />
    </div>
    <button type="submit">Submit</button>
</form>

JS

var form = $('#form'),
    el1  = '#'+form.find('#passM').attr('id'),
    el2  = form.find('#passR').attr('name');


form.validate({

    rules: {

          el2: {
            equalTo: el1
        }
    }
})

Code example

1 个答案:

答案 0 :(得分:2)

这会有用吗?

var form = $('#form'),
    el1  = '#'+form.find('#passM').attr('id'),
    el2  = form.find('#passR').attr('name');

var r = {};
r[el2] = { equalTo: el1 };

form.validate({
    rules: r
});