[英]JQuery and form attributes change in IE
I want to change form attributes with JQuery. 我想使用JQuery更改表单属性。 In other browsers it works fine, but not in IE(6,7,8). 在其他浏览器中,它可以正常工作,但在IE(6,7,8)中却不能。
Code: 码:
action = '/controller/action/id/';
target = 'upload_iframe';
enctype = 'multipart/form-data';
$('#form1').attr("action",action);
$('#form1').attr("target",target);
$('#form1').attr("enctype",enctype);
So what's the problem ? 所以有什么问题 ? Your help would be appreciated. 您的帮助将不胜感激。
Try this 尝试这个
$('#form1').attr("action","/controller/action/id/");
$('#form1').attr("target","upload_iframe");
$('#form1').attr("enctype","multipart/form-data");
I just tried this and it works in every browser for me .. 我只是试过了,它对我来说适用于所有浏览器..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btn").click(function() {
$('#form1').attr("action","/controller/action/id/");
$('#form1').attr("target","upload_iframe");
$('#form1').attr("enctype","multipart/form-data");
});
});
</script>
</head>
<body>
<form id="form1" action="something.html" method="post" name="forma" target="target" enctype="nope">
<a href="#" id="btn">Uno mas</a>
</form>
</body>
</html>
It works this way for me as well, just tested IE7 & 8 : 刚刚测试了IE7和8,它对我也是如此:
action = '/controller/action/id/';
target = 'upload_iframe';
enctype = 'multipart/form-data';
$('#form1').attr("action",action);
$('#form1').attr("target",target);
$('#form1').attr("enctype",enctype);
console.log($('#form1').attr("action") + " " + $('#form1').attr("target") + " " + $('#form1').attr("enctype"));
alert($('#form1').attr("action") + " " + $('#form1').attr("target") + " " + $('#form1').attr("enctype"));
You might have a field named "action": 您可能有一个名为“ action”的字段:
<input name="action" value="hot">
And IE, being retarted browser that it is, mixes the 2 up... 而IE,就是被拖延的浏览器,将两者混为一谈...
通过以下技巧(使用encoding
属性)设法使enctype
属性可在浏览器(FF,IE7 / IE8,Chrome)上运行:
$('#form1').get(0).encoding = 'multipart/form-data';
It seems, jQuery attr() method doesn't work in IE8 for enctype attribute. 看来,jQuery attr()方法在IE8中不支持enctype属性。 For example: 例如:
f=$('<form>');
f.attr({"target":"ta","id":"idx"});//OK - FF and IE8
f.attr("method","post");//OK - FF and IE8
f.attr("enctype","multipart/form-data");//OK in FF, but not in IE8!!!
IE8 doesn't send file content through upload. IE8不会通过上传发送文件内容。 But there is easy solution: 但是有一个简单的解决方案:
f=$('<form enctype="multipart/form-data" />');//FF and IE8
verification page: 验证页面:
<html>
<head>
<title>Example page for jQuery attr() bug - it can't set enctype attribute in form tag</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
//next two lines don't work in IE8, but they work in FF
f=$('<form />');
f.attr("enctype","multipart/form-data");
//comment two previous lines, uncomment next line and it work in IE8
//f=$('<form enctype="multipart/form-data" />');
f.attr("method","post");
f.append('<input type="file" name="ffile" id="ffile" /><input name="fsubmit" id="fsubmit" type="submit">');
$(document.body).append(f);
});
</script>
</head>
<body>
<p>
<?php
if(@basename(htmlspecialchars($_FILES['ffile']['name'])))echo "Browser has sent a file ".$_FILES['ffile']['name'];
else echo "Browser hasn't sent a file.";
?>
</p>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.