开发者

JQuery and form attributes change in IE

开发者 https://www.devze.com 2022-12-24 19:44 出处:网络
I want to change form attributes with JQuery. In other browsers it works fine, but not in IE(6,7,8). Code:

I want to change form attributes with JQuery. In other browsers it works fine, but not in 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",en开发者_如何学JAVActype);

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 :

    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":

<input name="action" value="hot">

And IE, being retarted browser that it is, mixes the 2 up...


Managed to get the enctype attribute working across browsers (FF, IE7/IE8, Chrome) through the following hack (using the encoding property):

$('#form1').get(0).encoding = 'multipart/form-data';


It seems, jQuery attr() method doesn't work in IE8 for enctype attribute. 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. 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消