Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - Ruby on rails radio_button_tag broke with onclick fucntion

I want to create a radio button with onclick function so when the user change status from open to close promp and alert message and asking for a reason, we have it on html + javascript and working fine so when we try to make it rails way, we put as default value open :checked => true so when we put the onclick: "comfirm();" rails ignore ** <%= radio_button_tag(:status, "Open", :checked => true) %> ** and put as checked the Close

   <%= radio_button_tag(:status, "Open", :checked => true) %>
   <%= label_tag(:status_ticket, "Open", id: "open") %>
   <%= radio_button_tag(:status, "Close") %>
   <%= label_tag(:status_ticket, "Close", id: "close") %> 

output

but when i want to add the on click function broke the thing

ignore the :checked => true and set as checked the close

like so

   <%= radio_button_tag(:status, "Open", :checked => true) %>
   <%= label_tag(:status_ticket, "Open", id: "open") %>
   <%= radio_button_tag(:status, "Close", onclick: "comfirm();") %>
   <%= label_tag(:status_ticket, "Close", id: "close") %>

output2

i don't think the function broke the things becouse is realy simple as you can see bellow

function comfirm() {
  var result = prompt('Why do you want to close the ticket?: ');
  document.getElementById("reason").value = result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The third parameter for radio_button_tag allows to indicate if the radio is checked, so onclick: "comfirm();" is interpreted as true. To make it work, you should add it as a fourth parameter:

radio_button_tag(:status, "Close", false, onclick: "comfirm();")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...