I have a form with a country select. The form should be extended with new input fields depending on the selected country.
Most of the tutorials i found on google are for ajax submits after filling a forum.
Can somebody recommend a tutorial/howto on howto extend a form via ajax on a select change?
Use something like this. Of cousre url, country_data, and custom_part must be handled according your app. And the returned data comes from your app, better without layout, only the part what you want to add.
$.get("url", country_data, function(returned_data){ $(".custom_part").html(returned_data) })
Related
In rails, I have a UserOffer model, that has_many Steps.
When I create a new UserOffer instance, I want that form to include a dropdown that says “select number of steps of that UserOffer”. Based on the number of steps selected (n), I want the form to expand, and include n extra fields, called “step 1 title”, “step 2 title” … “step n title”.
I figured I need to use nested attributed to do this, but I wanted to know how the form would look like, preferably without using JS or Ajax (just with RoR).
I would appreciate any help
If you want to do that, at the same form without refreshing the page, you will need to use an ajax request to the server, in order to add the steps to the UserOffer model. Remember that RoR only let you do things on the render step of the DOM, in other words, if you had rendered the form you will not be able to do another changes on the page without use javascript.
I'm trying to set a set of fields to be dynamically displayed on demand. In the model, I've the fields:
attr_accessible ... :instruct1, :instruct2, ... :instruct30
I would like the form to display just instruct1 with a button to add 1 more field until instruct30 is hit and a button to remove one until instruct 1 is hit. All should happen without refreshing page which i think would include some use of AJAX but I couldn't find anything that is similar.
I've searched for something similar but only able to come up with nested form which is not what im looking for as my model is fixed.
The majority of your work is going to be on the client side.
To add and remove form fields dynamically, you have to use javascript.
Check out the HTML that Rails generates for the first field, replicate that and add the additional fields using for example jQuery.
A crude example:
$("#button").click(function() {
$("#theForm")
.append('<input id="instruct2" name="object[instruct2]" type="text">');
});
You'd have to keep track of how many fields you've added or removed.
I'm using ExpressionEngine and SafeCracker along with Ajax (plugin: jquery.form.js - http://jquery.malsup.com/form/).
Best I can tell, SafeCracker will only allow for updating a single entry at a time. However, the UI / UX necessitates that a list be displayed. I've proof of concept'ed an entry by entry on-demand form. That is, click a particular edit link next to each entry and a snippet of jquery creates a form along with displaying a submit button. Click submit and that single entry updates. The inputs don't exist until the Update link is clicked
What I would prefer to do, if possible, is to create the non-form and form versions of each entry as the page is renbered and use some sort of toggle to display one or the other. Again, doable. Then, when I click the Edit link I'd add the necessary attributes to the input so that entry's form elements will be read but the other (display: none) elements for the other entries will be ignored. I'm thinking (out loud) that if I add the attr("name", some-value) that would work. That is, an input with no name will be ignored.
Yes, I can test this and I will. However, even if it works I'm not sure if it's a best practice and/or there's a more ideal way of accomplishing my ends. I'm here looking for validation and/or additional expertise and input.
Thanks in advance.
Just set disabled property to inputs and they will excluded from Form submission, whatever input fields are hidden or visible. Different jQuery methods, like submit() and serialize() follow specification of HTML 4 and exclude all disabled controls of a forms. So one way is to set
$('your_input').prop('disabled', true);
or ,
$('your_input').attr('disabled', 'disabled');
Check following link:
http://www.w3.org/TR/html401/interact/forms.html#successful-controls
Also, you may use a general button instead of a submit, as result you can handle click event on it and within that event you can make exclusion, validation, manipulation on values and what ever you like.
You can put a disabled attribute on them server side or set the property via jQuery:
$(".hidden input").prop("disabled", true);
Before I start work I would like to get some ideas. What I have is an MVC3 page that I currently use to display rows of data. There are many rows so I would like to filter them. Ideally at the top of my page I would like to have a select drop down box and a refresh button with rows of data appearing below when the refresh button is clicked.
I can imagine doing this with Ajax and then having the data from my controller populate new HTML text between a DIV.
Does this sound like the best approach? I am not looking for a person to write code for me. Just want to be sure my solution sounds like a good way to go.
thank you
i recommend this approach:
http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx
You can 'enchance' it with AJAX of course, but do not forget about users with disabled javascript. Make it work without client scripting, then enchance it, when its working.
I also think that you can simply create controller action, that is accepting parameters like pageNumber and amountOfItems. Then in your controls at page, you can just change values (number of page etc..) and use them in call for your controller action at form submit.
i just started building a small test app to help me learn MVC. i have a view that displays user comments. Under each comment i would like to have a reply action link. Clicking on the link should return a small form for adding a comment directly above the reply link.
What is the general approach for this? I'm imaging the form would be a partial view that i can somehow return using the reply link. Thanks for any help!
Using jQuery to retrieve and post the forms in partial views is how I would do it.
Just return partial view to be loaded by jQuery load:
$('#comment').load('/MyController/ActionReturnsPartial');
You should not have to retrieve anything from the server if the user is not providing any extra information along with the retrieval.
Instead of retrieving the form when the user clicks, just let the page render a form below each comment. Put the form in a div with style="display: none;". Then, when the user cliks the link, use jQuery to show the form. Something like
$('.commentlink').click(function(){
$(this).closest('div').find('.formdiv').show();
});
You may also be able to use jQuery's .toggle() method.