jQuery 选择器“内存”
我有一个包含多个字段的表单,每次用户更改字段时都会提交表单(通过隐藏的 iframe),并通过回调将响应放置在页面上适当的 div 中。第一次这个效果很好。但是在每个后续字段更改和提交中,响应都会显示在每个已填充响应的 div 中(因此它们都显示相同的内容,而不是所需的行为)。
谁能告诉我为什么会发生这种情况?似乎保留了之前调用过的选择器(自上次页面加载以来)......但我不确定。这是我的代码:
$(function ()
{
$('#ImageAddForm input').change(function (){
form = $('#ImageAddForm');
var fldDiv = $(this).parent().attr('id'); // eg Image11
var thDiv = fldDiv.replace('Image', 'Thumb'); // eg Thumb11
$(form).iframePostForm({
post : function (){
var msg = 'Uploading file...';
$("#" + thDiv).html(msg);
},
complete : function (response){
$("#" + thDiv).html(response);
$(':input', '#ImageAddForm').not(':hidden').val('');
}
});
form.submit();
});
});
I have a form with multiple fields, and each time the user changes a field the form is submitted (via hidden iframe), and the response is placed within an appropriate div on the page via a callback. The first time this works fine. But on each subsequent field change and submission, the response is shown in every div that has been filled with a response (so they all show the same thing, not the desired behavior).
Can anyone tell me why this is happening? It seems that there is some retention of the selectors that have been called before (since last page load)... but I'm not sure. Here's my code:
$(function ()
{
$('#ImageAddForm input').change(function (){
form = $('#ImageAddForm');
var fldDiv = $(this).parent().attr('id'); // eg Image11
var thDiv = fldDiv.replace('Image', 'Thumb'); // eg Thumb11
$(form).iframePostForm({
post : function (){
var msg = 'Uploading file...';
$("#" + thDiv).html(msg);
},
complete : function (response){
$("#" + thDiv).html(response);
$(':input', '#ImageAddForm').not(':hidden').val('');
}
});
form.submit();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉该插件,但我怀疑是什么导致了您的问题。您正在使用更改事件内的插件将一些功能附加到您的表单。这意味着每次更改时您都会再次附加,这可能会导致一些问题。建议采用两种解决方案:
1) 如果插件有某种取消绑定或销毁自身的调用,请在将插件绑定到表单之前调用该调用。这应该可以防止由多重绑定引起的任何奇怪行为。
2)更好的解决方案:将插件绑定到更改事件外部的表单,并确定变量(fldDiv、tdDiv)的范围,以便您的更改事件可以访问它们(以便可以根据更改的内容进行修改)以及插件使用的功能(用于发布和完成)。这样,您只需绑定插件一次,但仍然可以根据字段更改传递和接收不同的数据。
I'm not familiar with that plug-in, but I have a suspicion about what might be causing your problem. You are attaching some functionality to your form with the plug-in inside of your change event. This means that on every change you are attaching again, which is likely to cause some problems. Two solutions suggest themselves:
1) If the plug-in has some kind of call to unbind or destroy itself, call that right before binding the plug-in to the form. This should prevent any weird behavior caused by multiple binding.
2) Better solution: bind the plug-in to the form outside your change event, and scope your variables (fldDiv, tdDiv) such that they will be accessible to both your change event (so that they can be modified based on what changed) and the functions used by the plug-in (for post and complete). This way you will only bind the plug-in once, but can still pass and receive different data based on what field changed.