Javascript 如何获取span标签的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4280528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:42:26  来源:igfitidea点击:

How Do I get the values of span tags

javascriptjqueryhtml

提问by deepcell

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

如何获取 span 标记的值并将其发送到我的表单中到另一个页面?

<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..

我需要将我的 span 标签小计的内容发送到另一个页面,我想将它保存到一个隐藏字段中,但我发现没有办法做到这一点..

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; 
}

回答by kobe

this will give you value

这会给你价值

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

回答by John

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

.text()如果您使用 jQuery,则可以尝试该方法。

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

回答by Akhil das

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.

span 的值添加到 id="sub" 的输入字段中。您可以使用任何事件为隐藏字段分配值,例如单击、更改、提交,或者您按照逻辑进行操作。这是一个选项。

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

回答by Jasdeep Singh

Should'nt you be having subtotalinstead of totalthere?

你不应该在那里subtotal而不是在total那里吗?

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