不相关时如何删除元素的内容? [英] How can I remove the content of an element when is not relevant?
问题描述
如果我有一个包含三个选项 A、B、C 的单选按钮,并且我有一个输入字段,该字段的相关性取决于单选按钮是否具有值 A.那么:如果我在单选按钮中选择值 A,则输入字段可见,我可以添加一个值.但是当我将单选按钮更改为 B 时,输入字段被隐藏但值仍然存在(如果我再次选择 A,我可以看到该值)
If I have a radio button with three options A, B, C and I have an input field that the relevancy depends on if the radio button has the value A. Then: If I select the value A in the radio button, the input field is visible and I can add a value. BUT when I change the radio button to B, the Input field is hidden but the value is still there (if I select A again, I can see the value)
是否可以通过事件删除该值?
Is it possible to remove the value by an event?
推荐答案
最简单的就是用一个 calculate
重置值,你可以把它放在同一个 xf:bind
code> 用于在选择值 B 时隐藏输入,如下例所示.对于使用 Form Builder 创建的表单,有一个 RFE 用于实现此行为开箱即用,但仅在保存/提交时重置值,因此如果您在 A 和 B 之间来回切换,则输入中的值不会丢失,但在保存表单时会被清除/提交.
The simplest is to reset the value with a calculate
, which you can place in the same xf:bind
you use to hide the input when the value B is select, as done in the example below. For forms created with Form Builder, there is an RFE for this behavior to be implemented out-of-the-box, but reseting the value only on save/submit, so if you switch back and forth between A and B, the value in the input wouldn't be lost, but it is cleared when the form is saved/submitted.
<xh:html xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fr="http://orbeon.org/oxf/xml/form-runner">
<xh:head>
<xh:title>XForms Hello</xh:title>
<xf:model>
<xf:instance>
<instance>
<select1>A</select1>
<input/>
</instance>
</xf:instance>
<xf:bind ref="input"
relevant="../select1 = 'A'"
calculate="if (../select1 = 'A') then . else ''"
readonly="false()"/>
</xf:model>
</xh:head>
<xh:body>
<xf:select1 appearance="full" ref="select1">
<xf:item>
<xf:label>A</xf:label>
<xf:value>A</xf:value>
</xf:item>
<xf:item>
<xf:label>B</xf:label>
<xf:value>B</xf:value>
</xf:item>
</xf:select1>
<xf:input ref="input"/>
</xh:body>
</xh:html>
这篇关于不相关时如何删除元素的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!