翻译结果:处理 JavaScript,使加号 + 和减号 – 按钮正常工作。

huangapple go评论63阅读模式
英文:

Working Out The JavaScript Needed To Get A Plus + And Minus - Button Working

问题

以下是您要翻译的代码部分:

已经向我们网站上的产品页面添加了加号 + 和减号 - 按钮,通过更改以下代码行:

$the_button = ''<div class="max-qty">' . 
zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . 
        ''</div>
            <span class="qty-text">' . PRODUCTS_ORDER_QTY_TEXT . '</span>
            <input type="text" name="cart_quantity" value="' . 
                $products_get_buy_now_qty . 
            '" maxlength="6" size="4" aria-label="' . ARIA_QTY_ADD_TO_CART . '">
            <button type="button" class="minus" >-</button>
            <button type="button" class="plus" >+</button>';

完整的 PHP 代码如下:

<?php
if (CUSTOMERS_APPROVAL == 3 and 
    TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM == '') 
{
    // 什么也不做
} else {
?>
<?php
    $display_qty = (($flag_show_product_info_in_cart_qty == 1 
                    and $_SESSION['cart']->in_cart($_GET['products_id'])) 
        ? '<p>' . PRODUCTS_ORDER_QTY_TEXT_IN_CART . 
            $_SESSION['cart']->get_quantity($_GET['products_id']) . 
            '</p>' 
        : '');
    
    if ($products_qty_box_status == 0 or 
        $products_quantity_order_max == 1) 
    {
        // 隐藏数量框并默认为1
        $the_button = '<input type="hidden" name="cart_quantity" value="1">';
    } else {
        // 显示数量框
        $the_button = '<div class="max-qty">' . 
        zen_get_products_quantity_min_units_display((int)$_GET['products_id']) . 
        '</div>
        <span class="qty-text">' . PRODUCTS_ORDER_QTY_TEXT . '</span>
        <input type="text" name="cart_quantity" value="' . 
            $products_get_buy_now_qty . 
        '" maxlength="6" size="4" aria-label="' . ARIA_QTY_ADD_TO_CART . '">
        <button type="button" class="minus" >-</button>
        <button type="button" class="plus" >+</button>';
    }
    $the_button .= zen_draw_hidden_field('products_id', (int)$_GET['products_id']);
    $the_button .= zen_image_submit(BUTTON_IMAGE_IN_CART, 
                                    BUTTON_IN_CART_ALT, 
                                    ' id="addToCartButton"');
    $display_button = zen_get_buy_now_button($_GET['products_id'], $the_button);
?>
<?php 
    if ($display_qty != '' or $display_button != '') { 
?>
        <div id="cartAdd">
<?php
            echo $display_qty;
            echo $display_button;
?>
        </div>
<?php   
    } // 显示数量和按钮
?>
<?php 
} // CUSTOMERS_APPROVAL == 3 
?>
<!--eof Add to Cart Box-->
</div>
</div>

如果您需要帮助添加JavaScript代码,以使加号 + 和减号 - 按钮在按下时增加或减少默认数量1,请告诉我。

英文:

Have added a plus + and minus - button to the products page on our website, by changing the following line of code to:

$the_button = &#39;&lt;div class=&quot;max-qty&quot;&gt;&#39; . 
zen_get_products_quantity_min_units_display((int)$_GET[&#39;products_id&#39;]) . 
&#39;&lt;/div&gt;
&lt;span class=&quot;qty-text&quot;&gt;&#39; . PRODUCTS_ORDER_QTY_TEXT . &#39;&lt;/span&gt;
&lt;input type=&quot;text&quot; name=&quot;cart_quantity&quot; value=&quot;&#39; . 
$products_get_buy_now_qty . 
&#39;&quot; maxlength=&quot;6&quot; size=&quot;4&quot; aria-label=&quot;&#39; . ARIA_QTY_ADD_TO_CART . &#39;&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;minus&quot; &gt;-&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;plus&quot; &gt;+&lt;/button&gt;&#39;;

The full php code is:

&lt;?php
if (CUSTOMERS_APPROVAL == 3 and 
TEXT_LOGIN_FOR_PRICE_BUTTON_REPLACE_SHOWROOM == &#39;&#39;) 
{
// do nothing
} else {
?&gt;
&lt;?php
$display_qty = (($flag_show_product_info_in_cart_qty == 1 
and $_SESSION[&#39;cart&#39;]-&gt;in_cart($_GET[&#39;products_id&#39;])) 
? &#39;&lt;p&gt;&#39; . PRODUCTS_ORDER_QTY_TEXT_IN_CART . 
$_SESSION[&#39;cart&#39;]-&gt;get_quantity($_GET[&#39;products_id&#39;]) . 
&#39;&lt;/p&gt;&#39; 
: &#39;&#39;);
if ($products_qty_box_status == 0 or 
$products_quantity_order_max== 1) 
{
// hide the quantity box and default to 1
$the_button = &#39;&lt;input type=&quot;hidden&quot; name=&quot;cart_quantity&quot; value=&quot;1&quot;&gt;&#39;;
} else {
// show the quantity box
$the_button = &#39;&lt;div class=&quot;max-qty&quot;&gt;&#39; . 
zen_get_products_quantity_min_units_display((int)$_GET[&#39;products_id&#39;]) . 
&#39;&lt;/div&gt;
&lt;span class=&quot;qty-text&quot;&gt;&#39; . PRODUCTS_ORDER_QTY_TEXT . &#39;&lt;/span&gt;
&lt;input type=&quot;text&quot; name=&quot;cart_quantity&quot; value=&quot;&#39; . 
$products_get_buy_now_qty . 
&#39;&quot; maxlength=&quot;6&quot; size=&quot;4&quot; aria-label=&quot;&#39; . ARIA_QTY_ADD_TO_CART . &#39;&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;minus&quot; &gt;-&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;plus&quot; &gt;+&lt;/button&gt;&#39;;
}
$the_button .= zen_draw_hidden_field(&#39;products_id&#39;, (int)$_GET[&#39;products_id&#39;]);
$the_button .= zen_image_submit(BUTTON_IMAGE_IN_CART, 
BUTTON_IN_CART_ALT, 
&#39; id=&quot;addToCartButton&quot;&#39;);
$display_button = zen_get_buy_now_button($_GET[&#39;products_id&#39;], $the_button);
?&gt;
&lt;?php 
if ($display_qty != &#39;&#39; or $display_button != &#39;&#39;) { 
?&gt;
&lt;div id=&quot;cartAdd&quot;&gt;
&lt;?php
echo $display_qty;
echo $display_button;
?&gt;
&lt;/div&gt;
&lt;?php   
} // display qty and button ?&gt;
&lt;?php 
} // CUSTOMERS_APPROVAL == 3 
?&gt;
&lt;!--eof Add to Cart Box--&gt;
&lt;/div&gt;
&lt;/div&gt;

Still learning php, and haven't touched on any JavaScript yet. Could do with some help, if anyone knows a way of adding some JavaScript so that the plus + and minus - buttons, if pressed, will increase the default quantity of 1, or decrease, depending on which button is pressed.

答案1

得分: 1

以下是翻译好的内容:

<script type="text/javascript">
window.onload = function() {
    document.querySelector('.plus').addEventListener('click', function() {
        var qtyInput = document.querySelector('input[name="cart_quantity"]');
        qtyInput.value = parseInt(qtyInput.value, 10) + 1;
    });

    document.querySelector('.minus').addEventListener('click', function() {
        var qtyInput = document.querySelector('input[name="cart_quantity"]');
        qtyInput.value = parseInt(qtyInput.value, 10) - 1;
    });
}
</script>

编辑:一些解释:

  • window.onload - 这使您的代码在页面加载完毕后运行,
  • document.querySelector - 这是一个指向元素的指针,在这里我们指向了类名为 plus 的元素。但我建议使用 id 而不是类,因为类通常用于样式。当您有一个具体的、唯一的元素时,最好使用 id。更多信息请查看 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
  • addEventListener - JS 依赖事件,您可以添加各种事件,比如 onclick、onfocus、onmouseover(https://www.w3schools.com/js/js_events.asp)。第一个参数是事件名称,第二个是触发事件时要调用的函数。
  • qtyInput - 我们将一个指向元素的指针赋给了它,以便在下一行中使用,
  • 我们要么增加,要么减少输入元素的值。调用 parseInt 是可选的,它只是确保存储的是整数而不是字符串。

另外,我还建议将您的 PHP 代码与 HTML 和 JS 分开以提高可读性。这将有助于您在将来维护代码时。这意味着将函数的结果(如 get_quantity())分配给文件的开头变量,然后在您的 HTML 中只需嵌入变量。

稍后,当您更加自信时,我建议将您的 HTML 文件完全分开 - 元素应该具有它们的 id,比如 <div id="some_id">,然后添加一个独立的 JS 文件,用于处理这些 id 并分别操作它们。这个 JavaScript 理想情况下会与 PHP 通信以获取数据,然后使用数据更新您的 HTML。最后,一旦您学到更多内容,可以考虑学习如何在后端使用现代框架如 Symfony 或 Laravel,以及在前端使用 Vue/React/Angular。

英文:

Here, add it to your website, it is vanilla JS (can be a separate file and then you need this https://www.w3schools.com/tags/att_script_src.asp):

&lt;script type=&quot;text/javascript&quot;&gt;
window.onload = function() {
document.querySelector(&#39;.plus&#39;).addEventListener(&#39;click&#39;, function() {
var qtyInput = document.querySelector(&#39;input[name=&quot;cart_quantity&quot;]&#39;);
qtyInput.value = parseInt(qtyInput.value, 10) + 1;
});
document.querySelector(&#39;.minus&#39;).addEventListener(&#39;click&#39;, function() {
var qtyInput = document.querySelector(&#39;input[name=&quot;cart_quantity&quot;]&#39;);
qtyInput.value = parseInt(qtyInput.value, 10) - 1;
});
}
&lt;/script&gt;

EDIT: Some explanation of what is happening:

  • window.onload - this makes your code run when your page has finished loading everything,
  • document.querySelector - this is a pointer towards an element, in here we are pointing at the element class plus. I would recommend using id instead however as classes are most commonly used for styling. When you have a concrete, one element in mind, id's are the best. More here https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector.
  • addEventListener - JS works on events, you can add various events like onclick, onfocus, onmouseover (https://www.w3schools.com/js/js_events.asp). First argument is the event name, second is the function to be called when event triggers.
  • qtyInput - we assign a pointer to an element to be used in the next line,
  • we either decrease or increase the input element value. Call to parseInt is optional, it just ensures that what is stored is an integer and not a string.

On the side note I would also recommend separating your PHP code from HTML and JS for readability - it will help you in the future. Meaning assign results of the functions like get_quantity() to a variable at the beginning of a file, and then just embed the variable in your HTML.

LAter when you are more confident I would suggest that you have your HTML files completely separate - elements should have their ids like `<div id=some_id"> then add to it JS file that works with those ids and manipulates them separately. This javascript ideally would talk to php to get data and then it updates your HTML with it. Finally once you learn more definitely consider learning how to use modern frameworks like Symfony or Laravel on the backend side and Vue/React/Angular on the frontend side.

huangapple
  • 本文由 发表于 2023年5月25日 00:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定