简体   繁体   中英

Connecting HAML simple_form with javascript

I have something like this:

  = f.input :payment_date, as: :date
  = f.input :tuitor, as: :boolean, label: 'Tuitor payment'

and I want use on this a simple JavaScript like this:

costam = ->
if document.getElementById('datet').checked is true
  document.getElementById('pdate').disabled = false
else
  document.getElementById('pdate').disabled = true
return

How I can put 'datet' and 'pdate' in my HAML form? I was trying options like

:id => "pdate"

or

id: :pdate

but it's not working.

Looks like you have to use input_html :

= f.input :payment_date, as: :date, input_html: { id: "datet" }
= f.input :tuitor, as: :boolean, label: 'Tuitor payment', input_html: { id: "pdate" }

In regards your javascirpt, you'd be much better using variables:

costam = ->
  datet = document.getElementById('datet')
  pdate = document.getElementById('pdate')

  pdate.disabled = !datet.checked

You can see a working JSFiddle here:

var changer = function() {
  var datet, pdate;
  datet = document.getElementById('datet');
  pdate = document.getElementById('pdate');
  pdate.disabled = !datet.checked;  
};

$(document).ready(changer);
$("#datet").on("change", changer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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