Clearing using jQuery

Easy: you wrap a <form> around the element, call reset on the form, then remove the form using .unwrap(). Unlike the .clone() solutions otherwise in this thread, you end up with the same element at the end (including custom properties that were set on it). Tested and working in Opera, Firefox, Safari, Chrome and IE6+. … Read more

Form inside a table

A form is not allowed to be a child element of a table, tbody or tr. Attempting to put one there will tend to cause the browser to move the form to it appears after the table (while leaving its contents — table rows, table cells, inputs, etc — behind). You can have an entire … Read more

Stop form refreshing page on submit

You can prevent the form from submitting with $(“#prospects_form”).submit(function(e) { e.preventDefault(); }); Of course, in the function, you can check for empty fields, and if anything doesn’t look right, e.preventDefault() will stop the submit. Without jQuery: var form = document.getElementById(“myForm”); function handleForm(event) { event.preventDefault(); } form.addEventListener(‘submit’, handleForm);

Uploading both data and files in one form using Ajax?

The problem I had was using the wrong jQuery identifier. You can upload data and files with one form using ajax. PHP + HTML <?php print_r($_POST); print_r($_FILES); ?> <form id=”data” method=”post” enctype=”multipart/form-data”> <input type=”text” name=”first” value=”Bob” /> <input type=”text” name=”middle” value=”James” /> <input type=”text” name=”last” value=”Smith” /> <input name=”image” type=”file” /> <button>Submit</button> </form> jQuery + … Read more

POST a form array without successful

You need to generate the controls for the collection in a for loop so they are correctly named with indexers (note that property BatchProducts needs to be IList<BatchProductViewModel> @using (Html.BeginForm(“Save”, “ConnectBatchProduct”, FormMethod.Post)) { …. <table> …. @for(int i = 0; i < Model.BatchProducts.Count; i++) { <tr> <td>@Html.TextBoxFor(m => m.BatchProducts[i].Quantity)</td> <td>@Html.TextBoxFor(m => m.BatchProducts[i].BatchName)</td> <td> // add … Read more

How to prevent form from being submitted?

Unlike the other answers, return false is only part of the answer. Consider the scenario in which a JS error occurs prior to the return statement… html <form onsubmit=”return mySubmitFunction(event)”> … </form> script function mySubmitFunction() { someBug() return false; } returning false here won’t be executed and the form will be submitted either way. You … Read more