How to save a hash in the input form?

Say I have a hash and I want to enter it as val()

 $("#form_attribute").val( hash ) 

It is saved as the string "[Object, object]"

How to save it as a hash, and then allow the form to send this hash to my server?

+4
source share
2 answers

If you want to convert the object / value to a JSON string, you can use JSON.stringify to do something like this:

 $("#form_attribute").val(JSON.stringify(hash)) 

This is a built-in method for the latest browsers, which converts the object to the JSON notation representing it. If any browser does not support it, your page should have several polyfills to support


Literature:

+9
source

You can save it as a JSON string:

 $('#form_attribute').val(JSON.stringify(hash)); 

Or you can save the original object in the data attribute:

 $('#form_attribute').data('hash', hash); 
+4
source

All Articles