I'm writing a webserver app in clojure with Hiccup (and other things). I'm trying to have a check-box enable and disable two drop-down fields with a little JS but I can't make it work.
我正在用Hiccup(以及其他东西)在clojure中编写一个webserver应用程序。我正在尝试启用一个复选框,并使用一个小的JS禁用两个下拉字段,但我无法使其工作。
[:head
[:script "function toggleText(cb, t1, t2) {
document.getElementById(t1).disabled = !cb.checked;
document.getElementById(t2).disabled = !cb.checked;
}"]]
[:td (hf/check-box {:on-change (str "toggleText(" (name endtag) "," (name tytag) "," (name tmtag) ")")} endtag)]
[:td (hf/drop-down tytag (range 2013 2031) 2017)]
[:td (hf/drop-down tmtag (range 1 13) 6)]
2 个解决方案
#1
1
on-change
is a React handler and won't work in server-side HTML.
on-change是一个React处理程序,在服务器端HTML中不起作用。
If you don't want to create a separate JS file, you can use the onclick
attribute: the below should work (provided that the hf/check-box
function creates an element with the given properties):
如果您不想创建单独的JS文件,可以使用onclick属性:下面应该可以工作(假设hf / check-box函数创建一个具有给定属性的元素):
[:td (hf/check-box
{:onclick (str "toggleText(" (name endtag) ","
(name tytag) "," (name tmtag) ")")}
endtag)]
#2
0
Thanks Aleph, your fix together with adding getElementById for the cb parameter in the function did the trick!
感谢Aleph,你的修复以及为函数中的cb参数添加getElementById就可以了!
The function now looks like this
该功能现在看起来像这样
[:script "function toggleText(cb, t1, t2) {
document.getElementById(t1).disabled = !document.getElementById(cb).checked;
document.getElementById(t2).disabled = !document.getElementById(cb).checked;}"]
And I simplified the Hiccup code too (not passing the tags as parameters) so it looks like this
我也简化了Hiccup代码(不将标签作为参数传递),所以它看起来像这样
[:td (hf/check-box {:onclick (str "toggleText('endtag','toyear','tomonth')")} :endtag (some? (:toyear m)))]
[:td (hf/drop-down :toyear (range 2013 2031))]
[:td (hf/drop-down :tomonth (range 1 13))]
#1
1
on-change
is a React handler and won't work in server-side HTML.
on-change是一个React处理程序,在服务器端HTML中不起作用。
If you don't want to create a separate JS file, you can use the onclick
attribute: the below should work (provided that the hf/check-box
function creates an element with the given properties):
如果您不想创建单独的JS文件,可以使用onclick属性:下面应该可以工作(假设hf / check-box函数创建一个具有给定属性的元素):
[:td (hf/check-box
{:onclick (str "toggleText(" (name endtag) ","
(name tytag) "," (name tmtag) ")")}
endtag)]
#2
0
Thanks Aleph, your fix together with adding getElementById for the cb parameter in the function did the trick!
感谢Aleph,你的修复以及为函数中的cb参数添加getElementById就可以了!
The function now looks like this
该功能现在看起来像这样
[:script "function toggleText(cb, t1, t2) {
document.getElementById(t1).disabled = !document.getElementById(cb).checked;
document.getElementById(t2).disabled = !document.getElementById(cb).checked;}"]
And I simplified the Hiccup code too (not passing the tags as parameters) so it looks like this
我也简化了Hiccup代码(不将标签作为参数传递),所以它看起来像这样
[:td (hf/check-box {:onclick (str "toggleText('endtag','toyear','tomonth')")} :endtag (some? (:toyear m)))]
[:td (hf/drop-down :toyear (range 2013 2031))]
[:td (hf/drop-down :tomonth (range 1 13))]