简体   繁体   中英

Custom selection in Phoenix Framework template

I'd like to have a customselect field with 3 options: dog , cat , other - and when the other is selected I'd like the blank text input to appear below to allow user to write his favourite animal in it.

Is there a way to conditionally render field based on option selected?

<%= 
select f, :animal, [
   {"Dog", :dog},
   {"Cat", :cat},
   {"Other", :other}
]

text_input f, :animal
%>

You can't achieve it without the help of JavaScript.

HTML tags are rendered on the server side and the choice of option takes place on the browser.

If you don't want to write JavaScript code, try Phoenix LiveView , which allows you to change the DOM tree dynamically on the server side.

A JavaScript library works behind the Phoenix LiveView, but you will hardly notice it.

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