使用 jquery 验证下拉列表
我有一个注册表单,它使用 jquery 插件之一进行客户端验证。我可以验证文本框和其他控件,但下拉列表有问题。 我在注册页面上有一个字段
<asp:DropDownList ID="ddlSector" runat="server" class="validateCallback my_code_callback"></asp:DropDownList>
那些下拉列表输出
<select name="ddlSector" class="validateCallback my_code_callback">
<option value="244">--Choose--</option>
<option value="27">Hotel</option>
<option value="28">Motel</option>
<option value="29">Cottage</option>
</select>
,我
<script type="text/javascript" src="/scripts/uni-form-validation.jquery.js" charset="utf-8"></script>
<script type="text/javascript">
$(function () {
$('form.uniForm').uniform({
prevent_submit: true
});
});
window.my_code_callback = function (field, caption) {
if (field.val() === '244') {
return false;
}
}
</script>
在 jquery 文件中的主模板上有这样的实现,该文件进行验证有这样的示例
/**
* Callback validator
*
* Lets you define your own validators. Usage:
*
* <input name="myinput" class="validateCallback my_callback" />
*
* This will result in UniForm searching for window.my_callback funciton and
* executing it with field and caption arguments. Sample implementation:
*
* window.my_callback = function (field, caption) {
* if (field.val() === '34') {
* return true;
* } else {
* return caption + ' value should be "34"';
* }
* }
*
* @param jQuery field
* @param caption
*/
validateCallback : function (field, caption) {
var classes = field.attr('class').split(' '),
callback_function = '';
for (var i = 0; i < classes.length; i += 1) {
if (classes[i] === 'validateCallback') {
if (classes[i + 1] != 'undefined') {
callback_function = classes[i + 1];
break;
}
}
}
if (window[callback_function] != 'undefined' && (typeof window[callback_function] === 'function')) {
return window[callback_function](field, caption);
}
return i18n('callback', caption, callback_function);
}
};
我如何更改代码,以便如果访问者离开选项 --Choose--,它应该显示与其他控件一样的错误?
I have a registration form which uses one of jquery plugin for client side validation. I can validate textboxes and other controls but I have a problem with dropdownlist.
I have a field on registration page
<asp:DropDownList ID="ddlSector" runat="server" class="validateCallback my_code_callback"></asp:DropDownList>
Those dropdown list outputs that
<select name="ddlSector" class="validateCallback my_code_callback">
<option value="244">--Choose--</option>
<option value="27">Hotel</option>
<option value="28">Motel</option>
<option value="29">Cottage</option>
</select>
and I have such implementation on master template
<script type="text/javascript" src="/scripts/uni-form-validation.jquery.js" charset="utf-8"></script>
<script type="text/javascript">
$(function () {
$('form.uniForm').uniform({
prevent_submit: true
});
});
window.my_code_callback = function (field, caption) {
if (field.val() === '244') {
return false;
}
}
</script>
in jquery file which does validation has such example
/**
* Callback validator
*
* Lets you define your own validators. Usage:
*
* <input name="myinput" class="validateCallback my_callback" />
*
* This will result in UniForm searching for window.my_callback funciton and
* executing it with field and caption arguments. Sample implementation:
*
* window.my_callback = function (field, caption) {
* if (field.val() === '34') {
* return true;
* } else {
* return caption + ' value should be "34"';
* }
* }
*
* @param jQuery field
* @param caption
*/
validateCallback : function (field, caption) {
var classes = field.attr('class').split(' '),
callback_function = '';
for (var i = 0; i < classes.length; i += 1) {
if (classes[i] === 'validateCallback') {
if (classes[i + 1] != 'undefined') {
callback_function = classes[i + 1];
break;
}
}
}
if (window[callback_function] != 'undefined' && (typeof window[callback_function] === 'function')) {
return window[callback_function](field, caption);
}
return i18n('callback', caption, callback_function);
}
};
How could I change the code so if visitor leaves the option --Choose--, it should display the error like other controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
if($('#ddlSector').selectedIndex == 0)
可能有效吗?或者也许:
if($('#ddlSector').selectedIndex == 0)
might work?or maybe: