简体   繁体   English

从XForms操作调用JavaScript

[英]Call javascript from XForms action

I am experimenting with XForms and trying to dynamically load javascript, but cannot figure it out. 我正在尝试使用XForms并尝试动态加载javascript,但无法弄清楚。

I am presenting a simple example - that is just an input field and button that loads the javascript: 我正在展示一个简单的示例-只是一个输入字段和加载javascript的按钮:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms"
   xmlns:ev="http://www.w3.org/2001/xml-events" >
   <head>
      <title>Hello World in XForms</title>
      <xf:model>
         <xf:instance xmlns="">
            <data>
               <firstName/>
            </data>
         </xf:instance>
      </xf:model>

      <script type="text/javascript">
         var myFunction = function(){
           var name = document.getElementById("firstName").value;
           alert("Hello " + name + "!");
         }
      </script>
   </head>
   <body>
      <xf:label>Please enter your first name: </xf:label>
      <xf:input ref="firstName" id="firstName">         
      </xf:input>
      <br />
      <xf:trigger>
         <xf:label>Click me!</xf:label>
         <xf:action ev:event="DOMActivate">
            <xf:load resource="javascript:myFunction()" />
         </xf:action>
      </xf:trigger>
   </body>
</html>

So in my script I am trying to get the value from the input box and then show an alert box with concatenated string. 因此,在我的脚本中,我试图从输入框中获取值,然后显示带有串联字符串的警报框。 Currently, I get "Hello undefined!" 当前,我收到“ Hello undefined!”

Do you have an idea how to get the value from the firstName xf:input with Javascript? 您是否知道如何使用Javascript从firstName xf:input获取值? I know how to do it with XForms only, but this is sort of a proof of concept. 我只知道如何使用XForms做到这一点,但这只是一种概念证明。

On a side note - I am using XSLTForms, so the XForms runs on the client. 附带说明-我正在使用XSLTForms,因此XForms在客户端上运行。

Another hint might be in the fact that XSLTForms transforms the xf:input into several nested span elements with a <input type="text"> element, but that input element does not have a name or id. 另一个提示可能是XSLTForms将xf:input转换为带有<input type="text">元素的多个嵌套span元素,但是该input元素没有名称或id。

With XSLTForms, there are different possibilities... 使用XSLTForms,存在多种可能性...

If you want to access the value of the corresponding HTML input, I would suggest document.getElementById("firstName").xfElement.input.value . 如果要访问相应的HTML输入的值,建议使用document.getElementById("firstName").xfElement.input.value

You could also use the node property to get the value stored in the bound node. 您还可以使用node属性来获取存储在绑定节点中的值。

Don't hesitate to browse DOM with a debugger to find how to get things from XSLTForms! 不要犹豫,使用调试器浏览DOM,以了解如何从XSLTForms获取内容!

--Alain --Alain

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

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