javascript - How to access key/value data from form.submit()? -


in rails app i'm using jquery intercept form submit , perform functions on data.

the form response looks following:

"object"=>{"parent_id"=>"1", "array_of_strings"=>["string1", "string2", ......]} 

i need parent_id, , each of strings array_of_strings

$('#myform').submit ->   $this = $(this)   dataarray = $this.serializearray()    = 0   while < dataarray.length     if dataarray[i].name == 'object[parent_id]'       parent = dataarray[i].value   += 1    = 0   while < dataarray.length     if dataarray[i].name == 'object[array_of_strings][]'       string = dataarray[i].value       //do parent , string   += 1 

is best approach? seems overly comlpex iterate through entire form response access key/value. jquery/ javascript knowledge limited, , wonder if there better way more commonly used?

you can reference form field name or id attribute using jquery selectors , modify fields values using jquery's val() function retrieve or set form field values. i've wrote complete example shows how retrieve , modify form values on form submit , pass them (modified) server. of course, not way can it.

var form = $("#myform"); var parentidfield = form.find("[name=parentid]"); var someotherfield = form.find("[name=sameotherfield]"); form.on("submit",modifyformfieldsandsubmit);  function modifyformfields(submitevent) {    // retrieve field value    var parentidfieldvalue = parentidfield.val();    // modify parentidfieldvalue ... , set field    parentidfield.val("somenewvalue");    // same other fields want modify ...    // submit modified fields values server    // note 'form' reference jquery extended form , 'form[0]'    // reference underlying html form element    form[0].submit();        } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -