JQuery 验证:必需(依赖表达式)

发布于 2025-01-04 03:54:33 字数 1440 浏览 0 评论 0原文

我希望仅当选择特定选项或选择特定单选按钮或在文本输入字段中输入特定字符串时才需要某个字段(我可以找到的所有必需(依赖表达式)示例都假设依赖性是在文本字段的情况下输入是否被选中/取消选中或填充/未填充)

这是我需要的示例,基于单选部分:

婚姻状况(必填字段):

<input name="status" id='status'type="radio" value="couple" checked="checked" />
<input name="status" id='status'type="radio" value="single male"  />
<input name="status" id='status'type="radio" value="single female" />

合作伙伴姓名(依赖字段 -仅当选择“情侣”时才需要上面)

<input name="partner" type="text"  id="partner">

示例 2:必填字段 = 地点

<select name="venue" id="venue">
<option value="">Please select venue</option>
<option>London EC1</option>
<option>London E9</option>
<option>Birmingham</option>

等。 依赖字段=用户(仅上述选择之一需要,例如“伯明翰”)

<input name="user" type="text" id="user">

如何调整http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression中显示的示例以适应上述内容情况?

$("#myform").validate({
  rules: {
    details: {
      required: "#other:checked"
    }
  }, debug:true
});
$("#other").click(function() {
  $("#details").valid();
});

我已经尝试过:

user: {
      required: "#venue:('birmingham')"
    }

但是

partner: {
      required: "#status:('couple')"
    }

这些都会产生使用户和合作伙伴需要的效果,无论场地和状态如何

I want a field to become required only when a specific option is selected or a specific radio button is selected or when a specific string is entered in a text input field (All of the required( dependency-expression ) examples I can find assume that the dependency is whether an input is checked / unchecked or filled/unfilled in the case of text fields)

Here is an example of what I need, based on a radio section:

Marital Status (the required field):

<input name="status" id='status'type="radio" value="couple" checked="checked" />
<input name="status" id='status'type="radio" value="single male"  />
<input name="status" id='status'type="radio" value="single female" />

Partners Name (the dependent field - required only when 'Couple' is selected from above)

<input name="partner" type="text"  id="partner">

Example 2: Required field = Venue

<select name="venue" id="venue">
<option value="">Please select venue</option>
<option>London EC1</option>
<option>London E9</option>
<option>Birmingham</option>

etc.
Dependent field = user (only required for one of the above selections, say 'Birmingham')

<input name="user" type="text" id="user">

How do I adapt the example shown athttp://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression for the above situations?

$("#myform").validate({
  rules: {
    details: {
      required: "#other:checked"
    }
  }, debug:true
});
$("#other").click(function() {
  $("#details").valid();
});

I have tried:

user: {
      required: "#venue:('birmingham')"
    }

and

partner: {
      required: "#status:('couple')"
    }

but these have the effect of making user and partner required regardless of results in venue and status

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2025-01-11 03:54:33

对于第一个,您的无线电输入应该有不同的 id:

<input name="status" id='statusCouple' type="radio" value="couple" checked="checked" />
<input name="status" id='statusSingleMale' type="radio" value="single male"  />
<input name="status" id='statusSignleFemale' type="radio" value="single female" /> 

然后您的规则可以如下所示:

partner: {
  required: "#statusCouple:checked"
}

对于第二个,只需使您的规则如下:

user: {
  required: function(element) { return $("#venue").val() == 'Birmingham'; }
}       

应该可以解决问题。

For the first one, your radio inputs should have different id's:

<input name="status" id='statusCouple' type="radio" value="couple" checked="checked" />
<input name="status" id='statusSingleMale' type="radio" value="single male"  />
<input name="status" id='statusSignleFemale' type="radio" value="single female" /> 

Then your rule can look like this:

partner: {
  required: "#statusCouple:checked"
}

For the second one, just make your rule like this:

user: {
  required: function(element) { return $("#venue").val() == 'Birmingham'; }
}       

Should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文