简体   繁体   English

如何在Orbeon Forms的客户端JavaScript中获取元素ID?

[英]How to get element id in client-side javascript in Orbeon Forms?

I'm new to XHTML, XForm, and Orbeon, and I've got a question in regards to retrieving HTML element ID using client-side javascript in Orbeon Forms. 我是XHTML,XForm和Orbeon的新手,并且在使用Orbeon Forms中的客户端javascript检索HTML元素ID时遇到了一个问题。 In normal HTML I'd just used the getElementById function, and Orbeon's documentation suggest a function for retrieving the value of an element (controller in their terminology). 在普通的HTML中,我只使用了getElementById函数,Orbeon的文档提出了一个用于检索元素值的函数 (在其术语中为控制器)。

ORBEON.xforms.Document.getValue(controlIdOrElement) ORBEON.xforms.Document.getValue(controlIdOrElement)

However, with an Orbeon From (snippet) such as 但是,使用Orbeon From(摘要)例如

<xf:bind id="fr-form-binds" ref="instance('fr-form-instance')">
  <xf:bind id="section-1-bind" name="section-1" ref="section-1">
     <xf:bind id="cvr-input-bind" name="cvr-input" ref="cvr-input"/>

  (...)

<fr:section id="section-1-control" bind="section-1-bind">
  <xf:label ref="$form-resources/section-1/label"/>
    <fr:grid>
      <xh:tr>
        <xh:td>
          <xf:input id="cvr-input-control" bind="cvr-input-bind">
            <xf:label ref="$form-resources/cvr-input/label"/>
            <xf:hint ref="$form-resources/cvr-input/hint"/>
            <xf:alert ref="$fr-resources/detail/labels/alert"/>
          </xf:input>
        </xh:td>
  (...) 

the getValue function doesn't find a value on any of the parameter variants getValue函数未在任何参数变量上找到值

  • cvr-input CVR-输入
  • cvr-input-control CVR-输入控制
  • cvr-input-bind CVR-输入绑定

given as a string. 以字符串形式给出。

When I inspect the generated form I see that the input element has gotten the id section-1-control≡xf-383≡cvr-input-control , to which it makes sense that getValue cannot find it on the three variants above. 当我检查生成的表单时,我发现input元素已获得idsection-1-control≡xf-383≡cvr-input-control ,对于getValue在上述三个变体中找不到它是有意义的。

I've made a trigger button--shown below--that produces the intended result (map the input value to another controller/element), but I can't imagine the solution would have to involve hard-coding the section-parts of the element ID. 我已经制作了一个触发按钮(如下所示),该按钮可以产生预期的结果(将输入值映射到另一个控制器/元素),但是我无法想象解决方案将不得不对代码的各个部分进行硬编码。元素ID。

Thus my question is: how do I retrieve an element/controller (or its value) when the coded ID is modified in the final view? 因此,我的问题是: 在最终视图中修改编码ID后,如何检索元素/控制器(或其值)?

<xf:trigger id="get-pnumbers-btn-control" bind="get-pnumbers-btn-bind">
  <xf:label ref="$form-resources/get-pnumbers-btn/label"/>
  <xf:hint ref="$form-resources/get-pnumbers-btn/hint"/>
  <xf:alert ref="$fr-resources/detail/labels/alert"/>
  <xxf:script ev:event="DOMActivate" type="javascript">
    var cvrInputElementId = "section-1-control≡xf-383≡cvr-input-control";
    var cvrOutputElementId = "section-1-control≡xf-383≡cvr-output-control";
    var cvrInput = ORBEON.xforms.Document.getValue(cvrInputElementId);
    ORBEON.xforms.Document.setValue(cvrOutputElementId, cvrInput);
  </xxf:script>
</xf:trigger>

The xxf:client-id() function resolves an id and returns the client id. xxf:client-id()函数解析ID并返回客户端ID。 That could be helpful. 这可能会有所帮助。 Then you need somehow to pass that id to the script (which is easy with the upcoming 4.11 but a bit harder previously). 然后,您需要以某种方式将该ID传递给脚本(对于即将发布的4.11来说很容易 ,但是以前要难一些)。

You can also use jQuery, finding an element with id ending with a certain string: 您还可以使用jQuery,查找id以某个字符串结尾的元素:

var clientId = ORBEON.jQuery('[id $= "cvr-input-control"]').attr('id')

There is an RFE to improve the id scheme . 有一个RFE可以改进id方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM