2010-10-21 82 views
0

我有一个使用这个真棒插件的web表单,我试图设置一个规则。在我的表格中,我有一个数量字段。如果数量字段设置为1并且复选框被选中,那么我想验证一个字段。jQuery验证插件问题

我的形式设置,如:

Quantity  TEXTBOX  id = txtQuantity 
Gift?   CHECKBOX  id = #other 
Recipient  TEXTBOX  id = txtRecipient 

和我的jQuery代码是:

txtQuantity: "required", 
txtRecipientEmail: { required: "#other:checked" }, 

我想:

txtRecipientEmail: { required: "#other:checked", required: "$('txtQuantity').val() == '1'" } 

但不工作

有另一种方式来做到这一点?

回答

2

required can take a function as well,这是你想要的这里:

txtRecipientEmail: { 
    required: function() { 
    return $("#other:checked").length && $("#txtQuantity").val() == '1'; 
    } 
} 
+0

因为我要试试这个一小会儿,如果复选框被选中,我认为长度将返回1,否则为0,对不对? – coson 2010-10-21 18:04:05

+0

@coson - yup,绝对正确,如果它使得它更清晰,随时在里面粘贴'> 0',即使JavaScript以任何方式工作:) – 2010-10-21 18:05:26

+0

对!谢谢 – coson 2010-10-21 18:18:21