我想将变量用于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
}
}
})
答案 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
});