作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 JQuery Validation Plugin我想更改我的字段(邮政编码)的 minlength
和 maxlength
,这取决于国家/地区是否等于 PT(葡萄牙)。
但在验证中,结果总是:
谁知道我的问题是什么?
HTML:
<body>
<form id="myform">
<label for="field">Required, minimum 3(Other) or 9(PT): </label>
<input type="text" class="left" id="field" name="field"/>
<br/>
<select class="form-control" id="country" name="country">
<option value="0">-- Choose Country --</option>
<option value="BE">Belgium </option>
<option value="BR">Brazil </option>
<option value="ES">Spain </option>
<option value="FR">France </option>
<option value="NL">Dutch </option>
<option selected="selected" value="PT">Portugal </option>
</select>
<br/>
<input type="submit" value="Validate!"/>
</form>
</body>
JQuery:
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#myform").validate({
rules: {
field: {
required: true,
number: {
depends: function () {
return !($('select[name="country"]').val() != 'PT');
}
},
minlength: {
depends: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 3;
}
}
},
maxlength: {
depends: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 50;
}
}
}
}
}
});
最佳答案
答案已更新。
我做了一些更改,删除了depends
。
这是更新后的 fiddle 。 http://jsfiddle.net/Y2H9d/36/
$("#myform").validate({
rules: {
field: {
required: true,
number: function () {
return !($('select[name="country"]').val() != 'PT');
},
minlength: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 3;
}
},
maxlength: function () {
var country = $('select[name="country"]').val();
if (country == 'PT') {
return 9;
} else {
return 50;
}
}
}
}
});
关于javascript - JQuery 验证字段数长度取决于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863903/
我是一名优秀的程序员,十分优秀!