【问题标题】:Get variable value from input number从输入数字中获取变量值
【发布时间】:2017-04-14 03:31:35
【问题描述】:

我试图从数字输入中获取变量值,但没有成功。

HTML:

<input type="number" min="0" max="5" name="quantity" id="quantity" class="quantity" value="1" />

<div class="selectedvalue"></div>

脚本:

$().ready(function() {
var quantity = $('input[type=number][name=quantity]').val();
  $(".selectedvalue").html(quantity);
});

JSFIDDLE

我只能得到数字输入中的属性值(1),不能得到实际选中的值。

【问题讨论】:

    标签: input numbers


    【解决方案1】:

    查看 W3schools 中的示例。 在您的示例中,您使用值 1,并始终显示值 1,因为您在标签内设置。

    你不需要 jQuery 来获得这个。

    您输入的类型是数字,并从您的数字中添加 id 以通过带有document.getElementById("yourIdFromInput").value; 的 ID 获取值

    <!DOCTYPE html>
    <html>
    <body>
    
    Number: <input type="number" id="myNumber" value="2">
    
    <p>Click the button to display the number of the number field.</p>
    
    <button onclick="myFunction()">Try it</button>
    
    <p><strong>Note:</strong> input elements with type="number" are not supported in IE 9 and earlier versions.</p>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
        var x = document.getElementById("myNumber").value;
        document.getElementById("demo").innerHTML = x;
    }
    </script>
    
    </body>
    </html>
    

    带有innerHTML 的&lt;p&gt; 将在html 中显示该脚本的值。

    参考: 所有关于 here 的解释

    代码W3schools

    【讨论】:

    • 谢谢!!我可以用你的信息解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2011-09-27
    • 2012-09-09
    • 2016-09-30
    相关资源
    最近更新 更多