简体   繁体   English

使用jquery验证函数不适用于select元素

[英]validate function with jquery not working for select element

I've got working validate function for my input fields : 我已经为输入字段设置了验证功能:

$.tools.validator.fn("#password", function(input, value) {
    return value!='Password' ? true : {     
        en: "Please complete this mandatory field"
    };
});

$("#form").validator({ 
    position: 'bottom left', 
    messageClass:'form-error',
    message: '<div><em/></div>' // em element is the arrow
});

My input field : 我的输入字段:

<input type="password" id="password" class="full" value="" name="j_password" required="required" placeholder="Password" />

And tried the same logic for select but it's not working : 并尝试选择相同的逻辑,但它不起作用:

$.tools.validator.fn("#environment", function(select, value) {
    return value!='Select Environment' ? true : {     
        en: "Please choose the environment"
    };
});

My select (not working): 我的选择(不工作):

<select id="environment" style="opacity: 0; "><option value="Select Environment">Select Environment</option><option value="1">Dev</option><option value="2">Test</option></select>

My select (not working as well ): 我的选择(不正常):

<select id="environment" style="opacity: 0; "><option>Select Environment</option><option>Dev</option><option>Test</option></select>

Can someone point out where I'm making mistake and how to fix it ? 有人可以指出我犯错误的地方以及如何解决它?

Update: 更新:

I'm using http://jquerytools.org/documentation/validator/index.html 我正在使用http://jquerytools.org/documentation/validator/index.html

I don't know if this is relevant but when I want to get the item selected from a dropdown I use $('#dropDownID option:selected').val() . 我不知道这是否相关,但是当我想从下拉列表中选择项目时,我使用$('#dropDownID option:selected').val()

I would recommend displaying the value to see what you are actually getting, then try it this way. 我建议显示值以查看您实际获得的内容,然后以这种方式尝试。

Removed the style="opacity: 0;" 删除了style =“opacity:0;” and Your code seems to work fine, see: jsFiddle 你的代码似乎工作正常,请参阅: jsFiddle

如果你尝试使用$(select).val()来获取值,它应该从select中获得正确的选择值

Solved it, well actually I didn't I found solution here : 解决了它,实际上我没有在这里找到解决方案:

https://gist.github.com/1810536 https://gist.github.com/1810536

With following code : 使用以下代码:

 $.tools.validator.fn("#environment", function(input, value) {          
         return value!='Select Environment' ? true : {     
                en: "Please select which environment you want to logon"
            };
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM