如果出现“以上都不是”的情况,如何将 XForms 复选框标记为无效?选项被选中?

发布于 2024-10-10 11:35:23 字数 581 浏览 1 评论 0 原文

场景:

<xf:select appearance="full">
    <xf:item>
        <xf:label>Vanilla</xf:label>
        <xf:value>vanilla</xf:value>
    </xf:item>
    <xf:item>
        <xf:label>Strawberry</xf:label>
        <xf:value>strawberry</xf:value>
    </xf:item>
    <xf:item>
        <xf:label>None of the above</xf:label>
        <xf:value>none</xf:value>
    </xf:item>
</xf:select>

当选择“以上都不是”选项时,如果还选择了任何其他选项,则该控件应被标记为无效。我怎样才能实现这个目标?

Scenario:

<xf:select appearance="full">
    <xf:item>
        <xf:label>Vanilla</xf:label>
        <xf:value>vanilla</xf:value>
    </xf:item>
    <xf:item>
        <xf:label>Strawberry</xf:label>
        <xf:value>strawberry</xf:value>
    </xf:item>
    <xf:item>
        <xf:label>None of the above</xf:label>
        <xf:value>none</xf:value>
    </xf:item>
</xf:select>

When the "None of the above" option is selected, if any other option is selected as well, the control should be marked as invalid. How can I achieve this?

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

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

发布评论

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

评论(2

寂寞花火° 2024-10-17 11:35:23

您可以使用如下所示的 XPath 约束来完成此操作:它的

<xf:bind
    ref="my-value"
    constraint="
        if (tokenize(., '\s+') = 'none') then
            count(tokenize(., '\s+')) = 1
        else
            true()"/>

作用如下:

  • 如果选择了“无”值,则确保只有该值 (count() = 1)
  • 如果情况并非如此,
  • 则如果没有选择“无”选项,则该控件将被标记为无效,则该控件有效。

或者,或者此外,正如 Alex 在上面指出的那样,您可以在以下情况下自动取消选择其他选项:用户选择“none”选项:

<xf:bind
    ref="my-value"
    calculate="if (tokenize(., '\s+') = 'none') 'none' else ."
    readonly="false()"/>

这样做的作用是:

  • 如果选择了“none”值,则确保该值仅设置为“none”,
  • 否则,保持该值不变
  • 并确保该值是非只读(使用计算时这将是默认值)

更新2016-11-25:

原始解决方案也可以表示为:

<xf:bind
    ref="my-value"
    constraint=". = 'none' or count(tokenize(., '\s+')) != 1"/>

You can do this with an XPath constraint that looks like this:

<xf:bind
    ref="my-value"
    constraint="
        if (tokenize(., '\s+') = 'none') then
            count(tokenize(., '\s+')) = 1
        else
            true()"/>

What this does is the following:

  • if there is a "none" value selected, then make sure there is only that value (count() = 1)
  • if that's not the case, the control is marked as invalid
  • if there is no "none" option selected, then the control is valid

Alternatively, or in addition, as Alex points out about above, you could automatically deselect the other options when the user selects the "none" option:

<xf:bind
    ref="my-value"
    calculate="if (tokenize(., '\s+') = 'none') 'none' else ."
    readonly="false()"/>

What this does is:

  • if there is a "none" value selected, then make sure the value is set to "none" only
  • otherwise, keep the value as is
  • also make sure that the value is not readonly (which would be the default when calculate is used)

UPDATE 2016-11-25:

The original solution could also be expressed as:

<xf:bind
    ref="my-value"
    constraint=". = 'none' or count(tokenize(., '\s+')) != 1"/>
汐鸠 2024-10-17 11:35:23

contains() xpath 函数最适合您的要求。代码如下。

<xforms:bind nodeset="instance('myform')/node1"
     constraint="not(contains(.,'none'))" />

然而,正如其他人所说,如果即使用户选择它,您也不希望将其添加到您的选择中,则可以将“无”替换为“”。

contains() xpath function is best for your requirement. code will be as below.

<xforms:bind nodeset="instance('myform')/node1"
     constraint="not(contains(.,'none'))" />

However as others said, you can replace the 'none' with '' if you do not want it to be added to your selection even if the user selects it.

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