【问题标题】:IE8 is submitting the form to the current pageIE8 正在将表单提交到当前页面
【发布时间】:2010-09-02 19:31:44
【问题描述】:

它可以在所有其他浏览器上运行,之前也可以在 IE8 上运行,但我做了一些代码清理并移动了一些东西,现在它把这个表单提交给它自己。这是一个“ajax”上传器(是的,不是真正的 ajax,但你知道我的意思)

这里是 JS:

function file_upload($theform,item_id){
    $theform.attr('ACTION','io.cfm?action=updateitemfile&item_id='+item_id);
    if($theform.find('[type=file]').val().length > 0){
        $('iframe').one('load',function(){
            $livepreview.agenda({
                action:'get',
                id:item_id,
                type:'item',
                callback:function(json){
                    $theform.siblings('.upload_output').append('<li style="display:none" class="file-upload"><a target="blank" href="io.cfm?action=getitemfile&item_file_id='+json[0].files.slice(-1)[0].item_file_id+'">'+json[0].files.slice(-1)[0].file_name+'</a> <a style="color:red" title="Delete file?" href="#deletefile-'+json[0].files.slice(-1)[0].item_file_id+'">[X]</a></li>').children('li').fadeIn();
                    $theform.siblings('.upload_output').find('.nofiles').remove();
                }
            });
            //Resets the file input. The only way to get it cross browser compatible as resetting the val to nothing
            //Doesn't work in IE8. It ignores val('') to reset it.
            $theform.append('<input type="reset" style="display:none">').children('[type=reset]').click().remove();
        });
    }
    else{
        $.alert('No file selected');
        return false;
    }
}
/* FILE UPLOAD EVENTS */
//When they select "upload" in the modal
$('.agenda-modal .file_upload').live('submit',function(event){de
    file_upload($('.agenda-modal .file_upload'),$('.agenda-modal').attr('data-defaultitemid'));
});

如果您不知道,ACTION 必须大写才能使 Firefox 正常工作。另外,我确定它正在提交给它自己,因为 iframe 在它自己的内部显示当前页面,并且所有脚本都会再次加载。另外,在.live() 中,添加return false; 没有任何作用。

这是 HTML 以防万一:

        <label>Add Attachment</label>
        <form class="file_upload" method="post" enctype="multipart/form-data" target="upload_target" action="">
            <input name="binary" id="file" size="27" type="file" /><br />
            <br><input type="submit" name="action" value="Upload" /><br />
            <iframe class="upload_target" name="upload_target" src="" style="display:none"></iframe>
        </form>
        <label>Attachments</label>
        <ul class="upload_output">
        <li class="nofiles">(No Files Added, Yet)</li>
        </ul>

【问题讨论】:

  • 相关页面是否包含 BASE 标签?
  • 对于 Firefox,实际上不需要大写“action”属性的名称,一个简单的测试页面可以证明。
  • 不,它没有基本标签。对于动态变化的动作,它必须大写。我为此奋斗了好几天,而stackoverflow上的某个人实际上指出您必须将其大写。
  • @Pointy 是正确的 - action 对于 firefox 不需要是特殊的,您可以在此处使用 firebug 进行测试:jsfiddle.net/nick_craver/YGuhN
  • 这里:stackoverflow.com/questions/3400816/… 那不是动态添加的表单...

标签: javascript jquery ajax iframe file-upload


【解决方案1】:

它应该将表单发送给它自己。

考虑按钮类型为提交(即自动提交表单)并且表单操作为空(即,自行发送)。

但是,正如您所做的那样,提交处理程序可能会导致表单根本不提交(我问,首先提交的意义是什么?但这已经过去了)

无论如何,您应该停止事件传播并在您的事件处理程序中返回 false。

【讨论】:

  • 如果处理程序返回false,jQuery 将假定传播也停止。没有理由同时做这两件事(当然这并没有真正伤害任何东西)。
  • 只是为了尝试一下,我在现场活动结束时添加了 event.stopPropation()。什么都没有发生...即使在其他浏览器上,表单仍会提交。
  • Pointy 我遇到了一个问题,并停止输入文本框的 KeyDown。我建议同时使用。
  • 显示 iframe 怎么样?我已经看到有关隐藏 iframe 根本不运行的问题。要修复,请删除 style="display:hidden;"并确保 frameborder="0"、width="1" 和 height="1"
  • 嗯,必须以某种方式或某种方式修改了其他内容... iframe,我未显示:没有它,当我在 FF 或 Chrome 中上传某些内容时,它不会显示在里面( JSON 返回不),但文件已上传...
【解决方案2】:

您应该在“提交”处理程序的末尾return false;

啊——这很难理解。好吧,这个&lt;iframe&gt; 到底是在哪里定义的? 编辑——哦,我明白了。

再次编辑 — 好吧,我意识到我在这里的可信度有点像在薄冰上跳来跳去,但我建议在 &lt;iframe&gt; 中添加一个“id”属性(带有“upload_target”作为 id 值)。每隔一段时间,我就会准确地弄清楚“姓名”和“身份”中的哪一个是重要的(以及何时/为什么),但这些信息只持续了一个小时左右,然后我的大脑就会反常地忽略它。 (“重要”是指&lt;iframe&gt; 元素。)

【讨论】:

  • 返回 false 会使整个表单不为我提交。它仍然必须提交,只是到 iframe...
  • 其实你帮了我,告诉我不要太提交:) stackoverflow.com/questions/3367147/…
  • 哈哈!我觉得你的用户名听起来很熟悉 :-) 好吧,我还在摸索……
  • 是的,你帮了我好几次。并且很好地抓住了那个奇怪的 Firefox 东西......看起来很奇怪,它会让浏览器感到困惑。但很高兴知道。
  • 等待什么 jQuery 选择器引用“upload_target”?我在任何代码中都没有看到。 &lt;form&gt; 标签的“目标”应该是普通的“upload_target”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2016-03-29
  • 2016-08-09
  • 1970-01-01
相关资源
最近更新 更多