HTML元素动态

发布于 2025-01-27 19:54:07 字数 896 浏览 2 评论 0原文

我制作了一个输入号字段,并希望动态渲染在容器中的值,而无需重新加载页面。我为此使用了Ajax,但我仍然必须重新加载页面。

HTML:

        <form method="post">
            <div class="main">
                <label for="numb">Parcour</label><br><br>
                <input type="number" id="numb" min="1">
            </div>
            <div class="main" style="display: block;">
                <section id="erstellen">
                    
                </section>
            </div>
        </form>

JS:

    const xhr = new XMLHttpRequest();
    xhr.open("POST","dynamisch_ajax.html", true);

    xhr.onload = () => {
        if(xhr.status === 200)
        {
            $('#erstellen').html($('#numb').val())
        } else {
            alert('Irgendetwas past nicht')
        }
    }

    xhr.send();

I made a input number field and want to dynamic render the value of this in the container without reloading the page. I'm using ajax for this but I still have to reload the page.

HTML:

        <form method="post">
            <div class="main">
                <label for="numb">Parcour</label><br><br>
                <input type="number" id="numb" min="1">
            </div>
            <div class="main" style="display: block;">
                <section id="erstellen">
                    
                </section>
            </div>
        </form>

js:

    const xhr = new XMLHttpRequest();
    xhr.open("POST","dynamisch_ajax.html", true);

    xhr.onload = () => {
        if(xhr.status === 200)
        {
            $('#erstellen').html($('#numb').val())
        } else {
            alert('Irgendetwas past nicht')
        }
    }

    xhr.send();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2025-02-03 19:54:07

您必须使用预防违规方法来防止默认刷新:

$("form" ).submit(function( event ) {
  event.preventDefault();
  // The rest of the work
});

you must use from preventDefault method to prevent default refresh :

$("form" ).submit(function( event ) {
  event.preventDefault();
  // The rest of the work
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文