javascript - 提交时 - 页面刷新回到网页上的某个点?

标签 javascript php jquery html

我有一个联系表单,它是主页的一部分,当用户单击“联系我们”时,会弹出联系表单。我想要实现的是,当用户单击联系表单上的提交按钮时,页面将刷新,然后将用户直接带回到联系表单。

有没有一种方法可以使用 javascript/jquery/php 来完成此操作,或者使用 ID 作为 anchor ?

我在网上查看是否有任何直接答案,不幸的是我没有任何运气,所以你能提供的任何帮助都会很棒。另外一些示例代码也很棒:)

最佳答案

这样的代码可以工作:

$("#submit_form_button").click( function() {
    // we wait 1 second, so that the form has been submitted in ajax...
    setTimeout(function(){ location = window.location.href + "?action=display_form" }, 1000);

  // put "?action=display_form" or "&action=display_form" according the fact the page has already or not a ?xxx=yyy parameter
});

并将 submit_form_button 替换为提交按钮的 ID

因此,提交表单后,页面将使用新参数 action=display_form 进行刷新,然后您可以在页面中添加以下代码:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};


$( document ).ready(function() {
    if (getUrlParameter('action') =="display_form") {
         // i launch the JS code for redisplay me form here 
         // (using jquery to set the contact form CSS property to 'display: block;')
    }
});

关于javascript - 提交时 - 页面刷新回到网页上的某个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264185/

相关文章:

Javascript http请求-需要调用并存储结果进行处理

php - 选择时文本框更改

javascript - 返回包含特定单词的字符串的新数组

javascript - 使用react-native-ble-plx包发送数据

php - 如何抓取、排序和显示热门评论者 (PHP/MySQL)?

php - MySQL 嵌套 CASE 错误我需要帮助吗?

jquery - 使用 Infinitescroll jQuery 插件操作时丢失变量

javascript - jquery/javascript 或 css - 仅当显示可见时第 n 个子级的 css

javascript - WordPress:使用 get_header() 时,提交按钮上没有任何操作

javascript - 使用 for in 循环访问对象属性而不知道它的父对象