How Do I get the values of span tags

2020-08-23 01:28发布

问题:

How Do I get the value of a span tag and send it into my form to another page?

<span id="subtotal"></span>

I need to send the content of my span tag subtotal to another page, I would like to save it into a hidden field, but I found no way to do this..

I used this, but no success!

function getTotal() {
    return alert(document.getElementById('total').innerHTML);
}

Here goes the right function for those who need the answer! After I figured out the script ...

function getTotal() {
    //document.write(document.getElementById('total').innerHTML);
    var someValue = $(".total").text();
    alert("Value is "+someValue);
    //It cause to releoad the page and give me the parameter total          
    location.href="frmAdd.php?total=" + someValue; 
}

回答1:

this will give you value

$("#subtotal").text();


回答2:

You could try the .text() method if you're using jQuery.

$("#subtotal").text();


回答3:

The value of span is added to the input field of id="sub". you may use any events for assign values to hidden fields like click, change, submit, or you do as your logic .its an option.

$("#sub").val($("#subtotal").text());
<span id="subtotal"></span>
<input type="hidden" id="sub" >


回答4:

Should'nt you be having subtotal instead of total there?

function getTotal() {
    return alert(document.getElementById('subtotal').innerHTML);
}