简体   繁体   English

将值从按钮传递到JavaScript中的文本字段

[英]Pass a Value from Button to Text Field in JavaScript

I'm trying to pass a number to a text field on button click. 我试图将数字传递给按钮单击时的文本字段。 User hits "add to cart" and the 2499 is added to the total already in the text field. 用户点击“添加到购物车”,并将2499添加到文本字段中已存在的总数中。

<form name="cart">
    <input id="display" type="text" name="output" size="6" placeholder="0000"/>
<button id="scart" value="Add to Cart" onclick="addCart()" +='2499'"/>

<script type="text/javascript">

var total=""

function addCart(){
    document.getElementById('scart').value;
    total+= document.getElementById('display').value;
    console.log(total);
}

</script>

When learning, I feel that I understand the logic, but don't know the syntax. 学习时,我觉得我了解逻辑,但不了解语法。

You have some errors, your quotes are not matching and you don't have closing button tag. 您有一些错误,引号不匹配,并且没有关闭按钮标签。 Also, why don't you add 2499 in the code? 另外,为什么不在代码中添加2499?

How about this: 这个怎么样:

<form name="cart">
    <input id="display" type="text" name="output" size="6" placeholder="0000"/>
    <button id="scart" value="Add to Cart" onclick="addCart()" > 
    </button>

    <script type="text/javascript">

    var total=""

    function addCart(){
        document.getElementById('scart').value;
        total+= document.getElementById('display').value + 2499;
        console.log(total);
    }

    </script>
</form>

It's not clear what you are trying to do so I made the beginning of a "cart" button where the input is the quantity you want to add to cart. 目前尚不清楚您要做什么,因此我在“购物车”按钮的开始处输入了要添加到购物车中的数量。

I then print out the number typed in and the total if you pressed the button more than once. 然后,如果您多次按下按钮,我将打印出键入的数字和总数。

 <input id="display" type="text" name="output" size="6" placeholder="0000" /> <button id="scart" type="button">Add to cart</button> <script type="text/javascript"> var total = 0; var display = document.querySelector('#display'); var scart = document.querySelector('#scart'); scart.addEventListener('click', function() { var str = display.value; var num = parseInt(str, 10); console.log('You entered the number: ', num); if (!isNaN(num)) { total += num; } console.log('The total is now: ', total); }); </script> 

There are many things that is wrong here: 这里有很多错误的地方:

First, replace + with a good indicator data-value or whatever 首先,用良好的指标data-value或其他data-value替换+

<button id="scart" value="Add to Cart" onclick="addCart()" data-value="2499"/>

Second, In your script you should make var total a number not string . 第二,在脚本中,应使var totalnumber而不是string

var total = 0;

Finally, your addCart() function. 最后,您的addCart()函数。

function addCart() {
    // I see you are trying to get the value of the clicked button so you need to store it as this does not do anything
    // document.getElementById('scart').value;
    var value = document.getElementById('scart').value;
    // Add it to total var;, you need to parseInt it as .value returns string
    total += parseInt(value);
    // put it on display
    document.getElementById('display').value = total;
}

Hope that helps. 希望能有所帮助。

I think this is what you want. 我想这就是你想要的。 I made a codepen for you: https://codepen.io/NeilGBK/pen/boGadz?editors=1111 我为您制作了一个Codepen: https ://codepen.io/NeilGBK/pen/boGadz?editors = 1111

   <form name="cart">
        <input id="display" type="text" name="output" size="6" placeholder="0000"/>   
    </form>
    <button id="scart" onclick="addCart(2499)">Add To Cart</button>

<script>
    function addCart(v){
        document.getElementById('display').value = v
        console.log(v);
        return false;
    }
</script>

I'm passing the number to the function and simply using that to change the value of the input. 我将数字传递给函数,并简单地使用它来更改输入的值。 I'm also logging it to the console 我也将其记录到控制台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM