最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing number field attribute "max" value after success in jQuery - Stack Overflow

programmeradmin2浏览0评论

I'm still learning jQuery.

I have this form:

<form class="simple-checkout" enctype="multipart/form-data" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>">
    <input type="hidden" name="add-to-cart" value="<?php echo htmlspecialchars($getid); ?>"/>
    <input type="number" name="quantity" min="1" max=<?= $available_stock ?> value="1"/>
    <input type="hidden" id="thwepof_product_fields" name="thwepof_product_fields" value="order_date">
    <input type="hidden" id="order_date" name="order_date" value="<?php echo htmlspecialchars($get_day); ?>">
    <input type="hidden" name="action" value="create_ajax_checkout"/>
    <input type="submit" value="Add to cart"/>
</form>

I add this to this function:

add_action('wp_ajax_create_ajax_checkout', 'create_ajax_checkout');
function create_ajax_checkout()
{
wp_send_json_success($_POST);
}

And use this js file to handle the Ajax request:

jQuery(document).ready(function ($) {
$('.simple-checkout').ajaxForm({
    success: function (response) {
        console.log(response);
        //alert("Success");

    },
    error: function (response) {
        console.log(response);
        alert("Error!");
        }
    });
});

My question is that the $_POST['quantity'] has a max=<?= $available_stock ?>. I would like to reduce that default max value by the quantity of $_POST['quantity'].

So if the default quantity is 30 and you add 2 pieces to your cart then I would like to set the maximum available value to 28.

I'm still learning jQuery.

I have this form:

<form class="simple-checkout" enctype="multipart/form-data" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>">
    <input type="hidden" name="add-to-cart" value="<?php echo htmlspecialchars($getid); ?>"/>
    <input type="number" name="quantity" min="1" max=<?= $available_stock ?> value="1"/>
    <input type="hidden" id="thwepof_product_fields" name="thwepof_product_fields" value="order_date">
    <input type="hidden" id="order_date" name="order_date" value="<?php echo htmlspecialchars($get_day); ?>">
    <input type="hidden" name="action" value="create_ajax_checkout"/>
    <input type="submit" value="Add to cart"/>
</form>

I add this to this function:

add_action('wp_ajax_create_ajax_checkout', 'create_ajax_checkout');
function create_ajax_checkout()
{
wp_send_json_success($_POST);
}

And use this js file to handle the Ajax request:

jQuery(document).ready(function ($) {
$('.simple-checkout').ajaxForm({
    success: function (response) {
        console.log(response);
        //alert("Success");

    },
    error: function (response) {
        console.log(response);
        alert("Error!");
        }
    });
});

My question is that the $_POST['quantity'] has a max=<?= $available_stock ?>. I would like to reduce that default max value by the quantity of $_POST['quantity'].

So if the default quantity is 30 and you add 2 pieces to your cart then I would like to set the maximum available value to 28.

Share Improve this question edited Feb 8, 2018 at 9:54 Syscall 19.8k10 gold badges43 silver badges59 bronze badges asked Feb 8, 2018 at 9:34 DonSiteDonSite 732 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Give your field an ID (for example "myField") and then change the attribute value like this:

$('#myField').attr('max', '28');

And to get the value of your field, just don't pass a second parameter:

var myFieldVar = $('#myField').attr('max');

you can use set it by following code in your success function

$("input[type='number']").attr('max',response.quantity);

To get the current value

var currentValue = $('#idOfNumberField').attr('max');

To update with new Value :

$('#idOfNumberField').attr('max', newValue);
发布评论

评论列表(0)

  1. 暂无评论