-3

I'm building a kind of e-commerce site with jCart as a school project. I would like to do the following:

I would like to get the value of a hidden field and return it at another location on the same page.

While browsing Stack Overflow I found this code to get the value of the hidden:

<script> var subt = $('#jcart-subtotal').val();</script>

This works as expected. I checked it by using alert(subt).

I tried return(subt); instead of alert, because I want the value to be printed on the page itself, not shown in an alert message.

How should I do to print the value instead of have it shown in an alert popup?

4

3 回答 3

0

Put this wherever you want to show the value on page:

<span id='showMe'></span> 

JavaScript

$('#showMe').html($('#jcart-subtotal').val());
于 2013-05-08T14:35:06.157 回答
0

You can bind the returned value to any elements like

var subt = $('#jcart-subtotal').val();

Add one element in html file like this

<span id="grandTotal"></span> //or <label id="grandTotal"></label>

and finally,

$("#grandTotal").html(subt);
于 2013-05-08T14:36:53.347 回答
0

It depends on where you want your output to be put in.

Supposing you have a page structure with some DOM elements here and there, you just need to access the target DOM element you want and use its html() setter to do the job.

$("#myTargetSpan").html($('#jcart-subtotal').val());

Would put the value of your input into an element which has "myTargetSpan" id.

The return instruction is used when your code is called as a function, to return the value to the caller for further use.

于 2013-05-08T14:37:16.623 回答