来自ajax页面的值仅在jquery中第二次尝试时获得

时间:2018-07-31 12:03:08

标签: php jquery

第一次单击option框中的select标记不会获得值,如果第二次选择option值,则获取值。如果{{ 1}}用于alert内部。这是我的代码,请看看

on click

这是我的查看页面

if ($(element).is('select')) {

  var item = $(this).val();
  var total1 = $(this).closest('.near').find('.total');
  var url = "<?php echo base_url();?>admin_control/get_corresponding_quantity";
  $.post(url, {
    item: item
  }, function(data) {

    total1.html(data);
  });


  $(".item1").on('click', function(e)

    {

      e.preventDefault();
      var quantity = $(this).closest('.near').find(".quantity1").val();

      var total = $(this).closest('.near').find(".total1").val();
      alert(total);

      if (parseInt(quantity) > parseInt(total)) {
        $(this).closest('.near').find('.quantity_error').html('<span class="quantity_error_msg">Qty must be smaller than Total!..</span>').show();
        $("#btnCreate").prop('disabled', true);
      } else if (parseInt(quantity) <= parseInt(total)) {

        $(this).closest('.near').find('.quantity_error').hide();
        if ($('.quantity_error:visible').length < 1) {
          $("#btnCreate").prop('disabled', false);
        }
      }

    });


}

1 个答案:

答案 0 :(得分:0)

仅在从选择中选择一个值之后,您的脚本才会运行,这意味着eventhandler $(".item1").on('click', function(e)仅在选择一个值之后才设置。这就是为什么没有在第一个值选择上触发该事件的原因。

eventhandler移至document.ready,以便在第一个值选择中也触发该事件。