admin管理员组

文章数量:1202958

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I have products where I need to disable the quantity field on the product detail pages and dynamically change the quantity based on another value.

I change the value using this code:

if (jQuery('body').hasClass('is-adapt-quantity-product')) {
            jQuery('.wc-pao-addon-custom-textarea').bind('keyup', _.debounce(function(){
                jQuery(".qty").val(parseInt(jQuery(this).val().length));
            }, 500));
    }

This works fine. The quantity changes and if I click on the "add to cart" button, I have the quantity in my cart.

When I add this to the js code:

jQuery(".qty").attr("disabled", "disabled");

The quantity on the product detail page changes. However when I add the product to the cart, it adds always just the quantity of "1".

Does anybody know why this happens and how I can prevent this from happening? I want to disable the quantity field, because the field should be changes dynamically...

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 2 years ago.

Improve this question

I have products where I need to disable the quantity field on the product detail pages and dynamically change the quantity based on another value.

I change the value using this code:

if (jQuery('body').hasClass('is-adapt-quantity-product')) {
            jQuery('.wc-pao-addon-custom-textarea').bind('keyup', _.debounce(function(){
                jQuery(".qty").val(parseInt(jQuery(this).val().length));
            }, 500));
    }

This works fine. The quantity changes and if I click on the "add to cart" button, I have the quantity in my cart.

When I add this to the js code:

jQuery(".qty").attr("disabled", "disabled");

The quantity on the product detail page changes. However when I add the product to the cart, it adds always just the quantity of "1".

Does anybody know why this happens and how I can prevent this from happening? I want to disable the quantity field, because the field should be changes dynamically...

Share Improve this question asked May 28, 2022 at 15:28 TorbenTorben 2692 gold badges6 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If the input is disabled, it is ignored during form submission. You can try to use readonly attribute.

As it says in the docs: The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly

本文标签: Change quantity via jQuery and disabile quantity field