简体   繁体   English

在js_of_ocaml中使用textarea的值

[英]Use the value of a textarea with js_of_ocaml

I'm trying to use the value of a textarea with js_of_ocaml. 我正在尝试使用js_of_ocaml的textarea值。 Here is my source code : 这是我的源代码:

let matrix () =
  let text1 = get_textarea "input_1" in
  let btn1 = get_elem "btn_1" in
  text1##placeholder <- Js.string "Write your matrix !";
  btn1##textContent <- Js.some (Js.string "Calculate");
  btn1##onclick <- Dom_html.handler (if Matrix.is_matrix (Js.to_string text1##value) 
                                     then (let matrix = Matrix.get_matrix (Js.to_string text1##value) in
                                       fun ev -> (set_matrix_caracteristics (Matrix.nb_lines matrix) (Matrix.nb_columns matrix) (Matrix.determinant matrix) ; Js._false))
                                     else fun ev -> (error_input_matrix() ; Js._false))

I want to do some calculs on a matrix, and the matrix is write by the user through a textarea with an html interface. 我想对矩阵做一些计算,并且该矩阵是由用户通过具有html界面的textarea编写的。

I think the problem is that the value of text1 don't change, even if I write something in the textarea. 我认为问题是,即使我在textarea中写了一些东西, text1的值也不会改变。 Whatever the input on the textarea is, the result is the same. 无论文本区域上的输入是什么,结果都是相同的。

Do anyone know how to use the value write by the user ? 有谁知道如何使用用户写的值?

Thanks ! 谢谢 !

Your probleme here is that you evaluate text1##value before defining the handler function, so the value is evaluated when the handler is installed, and then never changed. 您的问题在于,在定义处理程序函数之前先评估text1##value ,因此该值是在安装处理程序时进行评估的,然后从不更改。 the following should works 以下应该工作

   btn1##onclick <- Dom_html.handler (
       fun ev -> 
                if Matrix.is_matrix (Js.to_string text1##value) 
                then let matrix = Matrix.get_matrix (Js.to_string text1##value) in
                                    (set_matrix_caracteristics (Matrix.nb_lines matrix) (Matrix.nb_columns matrix) (Matrix.determinant matrix) ; Js._false)
                else error_input_matrix() ; Js._false
      )

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

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