简体   繁体   中英

How do I use click logic in my .js.erb file to render different HTML depending on the button which was clicked?

I have a four step form that the user goes through to complete a process. Each step is a separate partial. I want to guide the user through the form using Ajax and decide which partial to display based on which step they are on in the process. I can tell which step they are on based on the button that is clicked. But the code below is not rendering anything in the browser once I add in the click logic. Things work fine when I take out the click listener.

$(function(){
    $("#ask2-btn").click(function(e){
        $("div#ask").html("<%= escape_javascript(render('asks/ask3')) %>")
    });

    $("#ask3-btn").click(function(e){
        $("div#ask").html("<%= escape_javascript(render('asks/ask4')) %>")
    });

});

UPDATE: Here is the response from the browser which isn't rendering

$(function(){
    $("#ask2-btn").click(function(e){
        $("div#ask").html("<p class=\"lead\">Help with this issue<\/p>\n<ul><li class=\"profile\">Sam Smith<\/li><\/ul>\n<em>\"Expected outcome of the ASK?<\/em>\n    <form accept-charset=\"UTF-8\" action=\"/asks/125\" class=\"edit_ask\" data-remote=\"true\" id=\"edit_ask_125\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"_method\" type=\"hidden\" value=\"put\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"2/vhLplWpnB5fSrkjgku27jzaJ0lwLK3GeOWchn6r8M=\" /><\/div>\n        \n        <input id=\"ask_category\" name=\"ask[category]\" type=\"hidden\" value=\"Advice\" />\n        <input class=\"btn btn-success btn-category\" name=\"commit\" type=\"submit\" value=\"Advice\" />\n<\/form>    <form accept-charset=\"UTF-8\" action=\"/asks/125\" class=\"edit_ask\" data-remote=\"true\" id=\"edit_ask_125\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"_method\" type=\"hidden\" value=\"put\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"2/vhLplWpnB5fSrkjgku27jzaJ0lwLK3GeOWchn6r8M=\" /><\/div>\n        \n        <input id=\"ask_category\" name=\"ask[category]\" type=\"hidden\" value=\"Introduction\" />\n        <input class=\"btn btn-success btn-category\" name=\"commit\" type=\"submit\" value=\"Introduction\" />\n<\/form>    <form accept-charset=\"UTF-8\" action=\"/asks/125\" class=\"edit_ask\" data-remote=\"true\" id=\"edit_ask_125\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"_method\" type=\"hidden\" value=\"put\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"2/vhLplWpnB5fSrkjgku27jzaJ0lwLK3GeOWchn6r8M=\" /><\/div>\n        \n        <input id=\"ask_category\" name=\"ask[category]\" type=\"hidden\" value=\"Support\" />\n        <input class=\"btn btn-success btn-category\" name=\"commit\" type=\"submit\" value=\"Support\" />\n<\/form>    <form accept-charset=\"UTF-8\" action=\"/asks/125\" class=\"edit_ask\" data-remote=\"true\" id=\"edit_ask_125\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"_method\" type=\"hidden\" value=\"put\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"2/vhLplWpnB5fSrkjgku27jzaJ0lwLK3GeOWchn6r8M=\" /><\/div>\n        \n        <input id=\"ask_category\" name=\"ask[category]\" type=\"hidden\" value=\"Money\" />\n        <input class=\"btn btn-success btn-category\" name=\"commit\" type=\"submit\" value=\"Money\" />\n<\/form>    <form accept-charset=\"UTF-8\" action=\"/asks/125\" class=\"edit_ask\" data-remote=\"true\" id=\"edit_ask_125\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /><input name=\"_method\" type=\"hidden\" value=\"put\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"2/vhLplWpnB5fSrkjgku27jzaJ0lwLK3GeOWchn6r8M=\" /><\/div>\n        \n        <input id=\"ask_category\" name=\"ask[category]\" type=\"hidden\" value=\"Don&#x27;t know\" />\n        <input class=\"btn btn-success btn-category\" name=\"commit\" type=\"submit\" value=\"Don&#x27;t know\" />\n<\/form>")
    });

    $("#ask3-btn").click(function(e){
        $("div#ask").html("<p class=\"lead\">Help with this issue<\/p>\n<ul>\n  <li class=\"profile\">Sam Smith<\/li>\n <li class=\"check\"><\/li>\n<\/ul>\n <hr/>\n <a href=\"/\">Add another ASK<\/a>")
    });

});

After several days of trying to figure this out, I decided to simply use Javascript on the client side (not Ajax) to render the four steps in the form without HTTP refresh. Rather than using four separate forms, I used one single form broken down into four separate div's. Div's 2 - 4 were hidden to start with and I wrote Javascript to listen for clicks on the "submit" button on each step in the process. On the click event, I copied the value from the field input and used the .text to write the value to the screen to give the user the impression it was saved to the database.

Based on my experience, I'm not sure that Rails and Ajax will do well to handle complex forms with multiple, consecutive update actions using Ajax.

Here is the Javascript:

<script>
    $("#ask1-btn").click(function(e){
        findDescription();
        $("div#ask-list").toggleClass("hidden");
        $("div.ask1").toggleClass("hidden");
        $("div.ask2").toggleClass("hidden");
        e.preventDefault();
    });

    $("#ask2-btn").click(function(e){
        askStep3();
    });

    function askStep3(){
        findContact();
        $("div.ask2").toggleClass("hidden");
        $("div.ask3").toggleClass("hidden");
    };

    function findDescription(){
        var ask_description = document.getElementById("ask-desc-input").value;
        $("li#ask-description p.cname").text(ask_description);
    };

    function findContact(){
        var ask_contact = document.getElementById("ask-contact-input").value;
        $("li#ask-contact").text(ask_contact);
    };

</script>

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