简体   繁体   English

单击按钮时,我的页面重新加载原因

[英]On click of the button my page reloads why

I have written a JavaScript code were in I have calendar and a button the code is properly running outside joget.我写了一个 JavaScript 代码在我有日历和一个按钮,该代码在 joget 之外正确运行。 While in joget application if I click on the button the page reloads, how can I stop this reloads of the page and display my actual on click function in joget.在 joget 应用程序中,如果我单击页面重新加载的按钮,如何停止重新加载页面并在 joget 中单击 function 时显示我的实际值。

在此处输入图像描述

`$('#getBetween').click(function () {
                var start = $("#date_picker1").datepicker("getDate"),
                    end = $("#date_picker2").datepicker("getDate"),
                    currentDate = new Date(start),
                    between = []
                    ;

                while (currentDate <= end) {
                    between.push(new Date(currentDate));
                    currentDate.setDate(currentDate.getDate() + 1);
                }

                $(document).ready(function () {
                    $("#results").html(between.map(function (value) {
                        return $('#results:last').after('<div class="block">' + value + '<button class="remove">x</button></div>')
                    }));
                    $('.remove').click(function () {
                        $(this).parent().remove();
                        console.log(between)
                    })
                });


            });

`

The getbetween is the function of my button. getbetween 是我的按钮的 function。 Ones I click on the button it displays the content at the same moment it refreshes the page, reloads the page in joget.我单击按钮,它在刷新页面的同时显示内容,在 joget 中重新加载页面。 While its properly working in Visual Studio.虽然它在 Visual Studio 中正常工作。 Can you please help me with this你能帮我解决这个问题吗

Can you add code: That happens when href is empty.您可以添加代码吗:当href为空时会发生这种情况。 change it to href:"#" .将其更改为href:"#"

Otherwise please add your code snippet.否则,请添加您的代码片段。


After updating the question here is what you can do.更新问题后,您可以做什么。

Way 1:方式一:

`$('#getBetween').click(function () {
                    var start = $("#date_picker1").datepicker("getDate"),
                        end = $("#date_picker2").datepicker("getDate"),
                        currentDate = new Date(start),
                        between = []
                        ;

                    while (currentDate <= end) {
                        between.push(new Date(currentDate));
                        currentDate.setDate(currentDate.getDate() + 1);
                    }

                    $(document).ready(function () {
                        $("#results").html(between.map(function (value) {
                            return $('#results:last').after('<div class="block">' + value + '<button  class="remove">x</button></div>')
                        }));
                        $('.remove').click(function (e) {
                         e.preventDefault()
                            $(this).parent().remove();
                            console.log(between)
                        })
                    });


                });

Just add preventdefault()只需添加preventdefault()

or Way 2 is not certain but you can add onClick= "" on button或方式 2 不确定,但您可以在按钮上添加onClick= ""

I think you must do prevent default behavior from your JS code.我认为您必须防止 JS 代码的默认行为。

for example:例如:

<form id="my-form">
    <!-- Your fields -->
    <input type="text">
    <button type="submit">Submit</button>
</form>

<script>
var myForm = document.querySelector("#my-form")
myForm.addEventListener('submit', (event)=> {
    event.preventDefault()
    // Do another stuffs after it
}) 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM