当调用getJSON时,防止滚动到顶部

Prevent scroll to the top when getJSON is called

本文关键字:滚动 顶部 调用 getJSON      更新时间:2023-09-26

我现在很困惑。

下面是我的代码:
    $("#combinations").on("change", "input", function (e) {
        e.preventDefault();
   console.log(e)
        var $button, $row, $group, $form, $barcodeValidation, url, barcodeVal;
        $button = $(this);
        $row = $button.closest(".input-group");
        $group = $row.closest(".form-group");
        $form = $group.closest("form");
        $barcodeValidation = $row.find("#barcode-validation");
        barcodeVal = $row.find("input").val();
        url = "/Product/CheckBarcode";
        var productNumber = $form.find("#Product_Number").val();
        $.getJSON(url, {
            barcode: barcodeVal,
            productNumber: productNumber
        }).done(function (result) {
            e.preventDefault();
        }).fail(function (result) {
        });
    });

所以当一些组合输入改变时,我想向服务器发出请求并检查一些。但由于某种原因,每次这个请求发生时,页面一直滚动到顶部。如果我注释Ajax请求部分,一切都很好。

我如何防止这种行为?

编辑

标记:

                    <div class="row">
                        <div class="col-md-12">
                            <div class="combinations">
                                @Html.EditorFor(m => m.Combinations)
                            </div>
                        </div>
                    </div>

编辑器包含一些输入字段。

The . preventdefault ();