问题
i want to save javascript object in a place, so if selected row is getting trigger, i can use that row object again by different methods.
maybe saving it in input hidden field can work? but not sure.. how would you do it?
im trying to do following, but that dont work, obviously my code is wrong, but i want to show you, so you can maybe tell proper way to do it.
<script>
jQuery(function($) {
var video = {title: 'this is title', time: '2:32:20'}
$('.arr').val(video);
$('.show').click(function() {
console.log($('.arr').val());
});
});
</script>
<input type="hidden" name="arr" class="arr" value="" />
<input type="button" class="show" value="Show" />
回答1:
JQuery has support for element data (see http://docs.jquery.com/Core/data). This allows you to set data in a "known" spot, like the document itself to be retrieved later.
$(document).data('foo','my data');
which can be retrieved as:
$(document).data('foo') // 'my data'
You are not limited to string values, objects can also be stored.
Hope this helps.
回答2:
If I understand right, you can save these objects you got from the "json server" using the jquery .data() method. If the object you want to save is in a variable named myobject and you want to save it with a DOM element with id "someid"
$('#someid').data('mydata', myobject);
saves the data. To retrieve:
var thedata = $('#someid').data('mydata');
来源:https://stackoverflow.com/questions/1975095/javascript-save-dynamic-object-in-someplace-for-resuse-byother-methods-later-on