JQuery ajax调用重置所有表单字段

时间:2022-12-01 10:18:36

I have been developing contact forms on my sites using captcha. An example can be seen here:

我一直在我的网站上使用captcha开发联系人表单。这里有个例子:

contact form

触点形式

Several years ago, if a user needed to reload the captcha image, the only choice I had then was to reload the entire form.

几年前,如果用户需要重新加载captcha图像,那么我唯一的选择就是重新加载整个表单。

Since I started using jquery, it was time to replace this method with one that would just refresh the image.

由于我开始使用jquery,现在是时候用一个只刷新图像的方法替换这个方法了。

So I wrote this little fragment:

所以我写了一小段:

$(document).ready(function() {
    $('#creload').click(function() {
        $.ajax({
            type: "POST",
            url: "/en/KDpg_Captcha.html",
            async: true,
            success: function(ret) {
                $('#cimg').attr('src', ret);
            }
        });
    });
});​

where #creload is the id of the link requesting the reload and #cimg is the id of the actual image.

其中#creload是请求重新加载的链接的id, #cimg是实际图像的id。

What I discovered is that if one fills in other form fields and then requests a reload, the other fields are reset. This is NOT a result to be desired.

我发现,如果一个填充了其他表单字段,然后请求重新加载,那么其他字段将被重置。这不是一个理想的结果。

What could I be missing?

我还会错过什么呢?

2 个解决方案

#1


2  

If #creload is a link you'll need to prevent it from doing it's default action which is opening what's in the href attribute. Adding e.preventDefault() stops this. Don't forget to add e as an argument:

如果#creload是一个链接,您需要阻止它执行它的默认操作,即打开href属性中的内容。添加e.preventDefault()停止。别忘了加e作为参数:

$('#creload').click(function(e) {
    e.preventDefault();
    // ...

#2


2  

When you click on the following link:

当你点击以下链接:

<a href id="creload">here</a>

the current page is interpreted as the href and the page is reloaded.

当前页面被解释为href,页面重新加载。

You can prevent this programmatically by adding function(e) and e.preventDefault() to your click handler as TJ describes, or more simply by changing the link to:

您可以通过添加函数(e)和e. preventdefault()来阻止此编程,如TJ所描述的单击处理程序,或者更简单地通过将链接更改为:

<a href="#" id="creload">here</a>

or, if you don't want the # link to take visitors to the top of the page:

或者,如果你不想让#链接把访问者带到页面的顶部:

<a href="javascript:" id="creload">here</a>

#1


2  

If #creload is a link you'll need to prevent it from doing it's default action which is opening what's in the href attribute. Adding e.preventDefault() stops this. Don't forget to add e as an argument:

如果#creload是一个链接,您需要阻止它执行它的默认操作,即打开href属性中的内容。添加e.preventDefault()停止。别忘了加e作为参数:

$('#creload').click(function(e) {
    e.preventDefault();
    // ...

#2


2  

When you click on the following link:

当你点击以下链接:

<a href id="creload">here</a>

the current page is interpreted as the href and the page is reloaded.

当前页面被解释为href,页面重新加载。

You can prevent this programmatically by adding function(e) and e.preventDefault() to your click handler as TJ describes, or more simply by changing the link to:

您可以通过添加函数(e)和e. preventdefault()来阻止此编程,如TJ所描述的单击处理程序,或者更简单地通过将链接更改为:

<a href="#" id="creload">here</a>

or, if you don't want the # link to take visitors to the top of the page:

或者,如果你不想让#链接把访问者带到页面的顶部:

<a href="javascript:" id="creload">here</a>