javascript方法执行两次 - bootstrap-slider

时间:2016-03-11 15:39:29

标签: javascript jquery uislider

我有动态生成的滑块,每个滑块都有给定的DOM ID slider{n}。 因为我不知道我将拥有多少个滑块,所以我已将on slideStop事件附加到类中,然后使用$(this)获取id,并执行与该滑块相关的任何操作。

$(".slider").on("slideStop", function(slideEvt) {

    var rowoffset = $(this).attr("id").substring(6);
    $("#percentage" + rowoffset).text(slideEvt.value);
    updateNumbers(rowoffset);

});

除了脚本执行updateNumbers()方法两次外,一切都按计划进行。

正如您在此处所见,我的postdata与每个响应一起记录到控制台两次,一个具有正确的值,第二个调用具有未定义的值。

很明显,第一个响应是对第二个电话的响应。

  1. 为什么我的方法会发射两次?是否与滑块库或jquery有关?
  2. 如果失败的呼叫总是在第二次执行,那么对失败的呼叫的响应是如何回来的?
  3. console

    这是updateNumbers方法,当然不会调用它自己。

    function updateNumbers(rowoffset){
    
      //get variables from DOM element values
    
      var percentage = $("#slider" + rowoffset).val();
      var group = $("#rrgroup").val();
      var rrclass = $("#rrclass").val();
      var category = $("#rrcategory").val();
      var subcategory = $("#subcategory" + rowoffset).val();
      var period = $("#rrperiod").val();
      var year = $("#rryear").val();
      var yearmonth = $("#rryearmonth").val();
      var rangesize = $("#rangesize" + rowoffset).text();
    
      var storesstring = $("#rrstores").val();
      if(storesstring == ""){
    
         storesarray = [];
    
      } else {
    
      var storesarray = storesstring.split(",");
    
      }
    
      var postdata = {percentage: percentage,
                      group: group,
                      class: rrclass,
                      category: category,
                      subcategory: subcategory,
                      period: period,
                      year: year,
                      yearmonth: yearmonth,
                      stores: storesarray,
                      rangesize: rangesize};
    
                      console.log(postdata);
    
             $.ajax({
              url: "ajaxrangemix.php",
              type: "post",
              dataType: 'json',
              data: postdata,
              success: function (response) {
    
                  console.log(response);
    
                  $("#dropskus" + rowoffset).text(response.skus);
                  $("#keepsales" + rowoffset).text(response.sales);
    
                  },
              error: function(jqXHR, textStatus, errorThrown) {
    
                  console.log(textStatus, errorThrown);
                }
    
    
          });
    
    }
    

    HTML

                <div class='panel-body' id='rangemixbody'>
    
                    <input type='hidden' id='rrgroup' value="<?php echo $group; ?>">
                    <input type='hidden' id='rrclass' value="<?php echo $class; ?>">
                    <input type='hidden' id='rrcategory' value="<?php echo $category; ?>">
                    <input type='hidden' id='rrperiod' value="<?php echo $period; ?>">
                    <input type='hidden' id='rryear' value="<?php echo $yr; ?>">
                    <input type='hidden' id='rryearmonth' value="<?php echo $yrmth; ?>">
                    <input type='hidden' id='rrstores' value='<?php implode(",", $storename); ?>'>
    
                    <table class='table table-striped'>
                        <thead>
                            <tr>
                                <th>Subcategory</th>
                                <th><span>0%</span><span class="pull-right">100%</span></th>
                                <th>%</th>
                                <th>Current Range Size</th>
                                <th>Drop Skus</th>
                                <th>Keep Sales</th>
                                <th>Sales Share</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        $i = 0;
                        $subcategories = array();
                        foreach($rangereviewarray as $row){
                        ?>
                            <tr>
                                <td>
                                    <input type="hidden" id="subcategory<?php echo $i;?>" value="<?php echo $row['subcategory']; ?>">
                                    <?php $subcategories[$i] = $row['subcategory']; ?>
                                    <?php echo $row['subcategory']; ?>
                                </td>
                                <td>
                                    <input id="slider<?php echo $i; ?>" type="text" class="slider" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100" data-slider-tooltip="hide">
                                </td>
                                <td>
                                    <span id="percentage<?php echo $i; ?>">100</span>%
                                </td>
                                <td>
                                    <span id="rangesize<?php echo $i; ?>"><?php echo $row['rangesize']; ?></span>
                                </td>
                                <td><span id="dropskus<?php echo $i; ?>">0</span></td>
                                <td>
                                    <input type="hidden" id="sales<?php echo $i; ?>" value="<?php echo $row['sales']; ?>">
                                    <span id="keepsales<?php echo $i; ?>"><?php echo number_format($row['sales'], 2); ?></span>
                                </td>
                                <td><?php echo number_format($row['share'], 2); ?></td>
                            </tr>
    
                        <?php
                        $i++;
                        }
                        ?>
                        </tbody>
                    </table>
                 <input type='hidden' id='rrsubcategories' value='<?php implode(",", $subcategories); ?>'>
                </div>
    

1 个答案:

答案 0 :(得分:1)

很难从问题中确定两次事件发生火灾的原因。根据附带的屏幕截图,我唯一的建议是当滑块与任何DOM元素无关时取消请求:

function updateNumbers(rowoffset){

    var $el = $("#slider" + rowoffset);
    if( $el.length == 0 ) {
         return;
    }

    //get variables from DOM element values

    var percentage = $el.val();
    var group = $("#rrgroup").val();
    var rrclass = $("#rrclass").val();
    var category = $("#rrcategory").val();
    var subcategory = $("#subcategory" + rowoffset).val();
    ..... rest of the function
}