【问题标题】:Can't set span contents to textbox input无法将跨度内容设置为文本框输入
【发布时间】:2014-11-09 08:33:57
【问题描述】:

我正在尝试将 SPAN 值的内容放入文本输入框中。

<script>
function myFunction()
{
  $('input[name=exp_total_copy]').val($('[name=document.getElementById("grandtotal").innerText]').val());
}

我也尝试过: $('input[name=exp_total_copy]').val($('document.getElementById("grandtotal").innerText').val());

文本框:

<input type="text" name="exp_total_copy" id="exp_total_copy">

跨度:

<span class="description" for="element_7"><b>Total: $</b><span name="grandtotal" id="grandtotal">0.00</span></span>

所以基本上当我的一个文本框 onClick 发生时,MyFunction() 被激活并应该抓取 grandtotal 跨度的内容并将其插入到 exp_total_copy 的文本框中。

【问题讨论】:

  • 您忘记向我们展示的地方是否有点击事件处理程序?
  • David,从另一个文本框单击时,onClick="myFunction()"。然后在其中,它会抓取其他数据来更新跨度并完美地完成。

标签: javascript jquery html css


【解决方案1】:

您没有执行查询;您将其作为字符串提供。

$('input[name=exp_total_copy]').val($('[name='+document.getElementById("grandtotal").innerText+']').val());

但是为什么要麻烦 DOM 查询呢?只需使用 jQuery:

$('input[name=exp_total_copy]').val($('[name='+$("#grandtotal").text()+']').val());

【讨论】:

    【解决方案2】:
        You just complicated it more. what work should be
    
         function myFunction(){
            var spanText = $("#grandtotal").text();
            $("input[name=exp_total_copy]").val(spanText);
        }
    
       OR
    
        //using the Id attributes
        function myFunction(){
                var spanText = $('#grandtotal').text();
                $('#exp_total_copy').val(spanText);
         }
    

    【讨论】:

    • 出于某种奇怪的原因,它也没有更新。 privates.nitetours.com 是页面。当您更改价格时,男性/女性一直计算底部的框没有任何价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    相关资源
    最近更新 更多