【问题标题】:How to pass class value from jquery into input box如何将类值从jquery传递到输入框
【发布时间】:2019-10-21 07:11:05
【问题描述】:

index.php

 <span class="final_build_price" data-from="0" data-to="" data-speed="500" data-refresh-interval="10">0</span>
<form method="post" action="">
    <input type="text" id="finalvalue" value="">
    <input type="submit" class="getquote" value="submit" name="send">    
</form>

js

<script>
    $(".dim_price").countTo();
    $(".final_build_price").countTo();
    var finalpice=$(".final_build_price").countTo()
    $('.getquote').click(function(){
        // var fp = JSON.stringfy(finalpice);
        var fp =parseInt(finalpice);
        alert(fp);
        // document.getElementById("final_price").innerHTML=fp;
    });
</script>

<script>
    $(".dim_price").countTo();
    var finalpice=$(".final_build_price").countTo()
    $('.getquote').click(function(){
        // var fp = JSON.stringfy(finalpice);
        var fp =parseInt(finalpice);
        alert(fp);
        document.getElementById("final_price").innerHTML=fp;
    });
</script>

在跨度类(final_build_price)中,我从下面提到的 jquery 中获取了一个值,但我想在单击提交按钮时将该值传递到输入框中,下面提到了我使用的 javascript 代码。但我没有将任何数据输入输入框

【问题讨论】:

  • 检查您在 var finalprice 中获得的值。使用 console.log
  • 它给出的输出是 NAN
  • 我猜你做错了,尝试使用 .html 或 .text() 访问你的类的值 例如:$(".final_build_price").html()
  • 我们无法对此进行测试,因为您正在使用插件。请制作一个可以重现您当前情况的工作代码 sn-p。
  • 您似乎没有任何 ID 为 final_price 的元素 - 您有 finalvalue。输入字段的值是通过将其分配给value 属性来设置的,或者在jQuery 中使用.val(),而不是innerHTML 或.html() 或类似的东西。

标签: javascript php jquery json


【解决方案1】:

此问题是由于将 id 写入 class 和元素名称。

替换

document.getElementById("final_price").innerHTML=fp;

$(".final_build_price").html(fp);

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多