javascript - 隐藏输入需要文本

标签 javascript jquery html

有人帮我弄到了这段代码。

<input type="hidden" id="charge_total" value="charge_total">

还有这个 JavaScript

<script>
document.getElementById("charge_total").value = document.getElementById("system_cost").innerHTML;

我的问题是我使用的主机支付系统要求隐藏的输入有一个名字。

所以隐藏的输入一定是这样的

<input type="hidden" id="charge_total" name="charge_total" value="charge_total">

我的问题是使用代码时它没有在该字段中输入任何文本,我在结帐页面中留下了一个空白的总数。我需要这个 span ID 来输出它在这行代码中输入的隐藏值中的任何数字。

<p>Total: <span id="system_cost">&nbsp;</span></p>

<input type="hidden" id="charge_total" name="charge_total" value="charge_total">

谁知道我做错了什么?


这是我将它放在页眉中的方式。

<script>
$(function(){ //on document ready attach handlers
$('form').on('submit', function(e){
    e.preventDefault(); // stop the form submitting and do work
    $('#charge_total').val(parseFloat($('#system_cost').text()));
    $(this).submit(); //now submit the form, calculations are done
});
});
</script>

这是另外两行。

<input type="hidden" name="charge_total" value="charge_total">

<p>Total: <span id="system_cost">&nbsp;</span></p>

跨度显示在页面上,就像它假设的那样并正确填充。只有隐藏值才显示 charge_total。

最佳答案

$(function(){ //on document ready attach handlers
    $('form').on('submit', function(e){
        e.preventDefault(); // stop the form submitting and do work
        $('#charge_total').val(parseFloat($('#system_cost').text()));
        this.submit; //now submit the form, calculations are done
    });
});

关于javascript - 隐藏输入需要文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592217/

相关文章:

javascript - 替换字符串索引处的字符

javascript - react 可编辑表格

javascript - 我使用 MDL 得到 2 个滚动条,我该如何修复它?

javascript - 处理 ContentEditable 区域中的图像点击事件

html - 如何使用 IdSMTP (Delphi) 发送包含 html 内容的电子邮件?

javascript - 彩盒 - 外部链接

javascript - 在某些字段中设置异常(exception)

javascript - 物中物

javascript - 如何使div内容向右溢出时隐藏

html - 在 IE 中垂直对齐 <th>