jQuery验证& lt; select& gt;分钟取决于其他领域,不起作用 [英] JQuery Validation <select> min depends on other field, not working

查看:86
本文介绍了jQuery验证& lt; select& gt;分钟取决于其他领域,不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择的"ArrearsLHOLocation",它的选项"None Selected"的值为-1,所有其他可选值均大于0.

I have a select "ArrearsLHOLocation" that has an option "None Selected" with a value of -1 and all other selectable values are greater than 0.

这是我的javascript中定义的规则

Here is the rule defined in my javascript

    $("#ArrearsLHOLocation").rules("add", {
        min: {
            depends: function (elem) {
                return $("#rbArrearsLHO[value='True']").is(":checked") ? 0 : -1;
            }
        },
        messages: {
            min: "You have stated that the applicant/coapplicant have LHO arrears but you have not stated where!"
        }
    });

以下是在Chrome中观察到的手表.

Here are the watches as observed in Chrome.

 $("#rbArrearsLHO[value='True']").is(":checked") ? 0 : -1: 0
 $("#rbArrearsLHO[value='True']").is(":checked"): true
 $("#ArrearsLHOLocation").val(): "-1"
 $("#ArrearsLHOLocation").val() >=($("#rbArrearsLHO[value='True']").is(":checked") ? 0 : -1): false

此页面错误地提交了成功.如果切换单选按钮,手表将显示如下:

This page successfully submits, incorrectly. If I toggle the radio button and the watches appear like this:

 $("#rbArrearsLHO[value='True']").is(":checked") ? 0 : -1: -1
 $("#rbArrearsLHO[value='True']").is(":checked"): false
 $("#ArrearsLHOLocation").val(): "-1"
 $("#ArrearsLHOLocation").val() >=($("#rbArrearsLHO[value='True']").is(":checked") ? 0 : -1): true

此页面已停止提交,并显示我的错误消息.

This page is stopped from being submitted and my error message is displayed.

我在做什么错了?

史蒂夫

-按要求的HTML片段-

--Snippet of HTML as requested--

<td>Do they have arrears?</td><td>
Yes <input id="rbArrearsLHO" name="rbArrearsLHO" type="radio" value="True" />
No <input checked="checked" id="rbArrearsLHO" name="rbArrearsLHO" type="radio" value="False" />
</td>
</tr>
<tr>
 <td colspan="2">
   <blockquote>
    If YES, Where? 
       <select data-val="true" data-val-number="The field ArrearsLHOLocation must be a number." id="ArrearsLHOLocation" name="ArrearsLHOLocation">
            <option value="-1">None Selected</option>
            <option value="4">Location 1</option>
            <option value="6">Location 2</option>
            <option value="5">Location 3</option>
            <option value="7">Location 4</option>
       </select>
   </blockquote>
 </td>
</tr>

推荐答案

查看jquery.validate时发现,depends仅应评估为true/false.因此,将min修改为具有[param:0],这将是我需要时的min.然后,当最小值应有效时,将depends值更改为true即可解决此问题.

Looking at jquery.validate I found that depends should only ever evaluate to true/false. So modifying the min to have [param : 0] which would have been the min when I needed it. Then changing the depends to evaluate to true when the minimum should be in effect solved this issue.

$("#ArrearsLHOLocation").rules("add", {
        min: {
            param: 0,
            depends: function (elem) {
                return $("#rbArrearsLHO[value='True']").is(":checked");
            }
        },
        messages: {
            min: "You have stated that the applicant/coapplicant have LHO arrears but you have not stated where!"
        }
    });

演示: FIDDLE (由@Sparky的上一个小提琴分叉)

DEMO: FIDDLE (forked from @Sparky's previous fiddle)

这篇关于jQuery验证&amp; lt; select&amp; gt;分钟取决于其他领域,不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆