Show Hidden Field Or Neew Request With Ajax?
I need insert some input fields into a form on a website. These fields will be inserted depending on the option that the user chooses in a
Solution 1:
Ajax is for "talking" to the server. If it's just a case of change a <select>
value and show/hide some fields, then put them on the page with style='display:none;'
and show/hide them by changing that style, eg with jquery you can use:
$(selector).show();
Some example code (there are, of course, many ways to do this, here's one):
$("#picker").on("change", function() {
$(".dogs,.cats").hide();
$("." + $(this).val()).show();
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Do you like:
<selectid='picker'><optionvalue=''>please select</option><optionvalue='dogs'>dogs</option><optionvalue='cats'>cats</option></select><divclass='dogs'style='display:none;'>
which sort of dog:
<select><option>big</option><option>sloppy</option><option>yappy</option></select></div><divclass='cats'style='display:none;'>
what type of cat:
<select><option>aloof</option><option>independent</option><option>house cat</option></select></div>
Solution 2:
If you really need to do something on the server when select field is changed, you may need to use ajax calls, if you don't just hide the fields and show them depending on the select option.
Post a Comment for "Show Hidden Field Or Neew Request With Ajax?"