在javascript / jquery中触发html文件 - 浏览器单击事件?

时间:2014-03-14 14:43:03

标签: javascript jquery

这个脚本有什么问题?我试图触发无法正常工作的文件浏览器onclick事件。

...谢谢

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script type="text/javascript" src="jquery-v2.0.3.min.js"></script>
  <script type="text/javascript">
  function foo()
  {
      //height:0px;overflow:hidden;
        if ($('#WebFormFileUploadMalsup').length == 0) {
            $('body').append('<div style=""><form id="WebFormFileUploadMalsup" name="WebFormFileUploadMalsup" method="post" action="../websrvc/PhotoServerIntegration.ashx" enctype="multipart/form-data"><input type="hidden" id="WhichWebAction" name="WhichWebAction" value="VehicleImagesUpload" /><input type="file" id="FormButtonFileBrowserMalsup" name="myfile[]" multiple onChange="document.getElementById(\'FormButtonFileUploadMalsup\').click();" OnClick="alert(\'Please click button to upload after you are done browsing with files.  Maximum file-size is 8 MB.\');"><input type="submit" id="FormButtonFileUploadMalsup" value="Upload File to Server"></form></div>');
        }


        //alert(document.getElementById('FormButtonFileBrowserMalsup'));
        //if (!(document.getElementById('FormButtonFileBrowserMalsup')))
        if ($('#FormButtonFileBrowserMalsup'))
        {
            alert("Error");
        }
        else
        {
            //#//Document.getElementById() works better than using JQuery's on() click event...
            //#//$('#FormButtonFileBrowserMalsup').on("click", function () { alert("ggg"); });
                              //#/document.getElementById('FormButtonFileBrowserMalsup').onclick = fucntion() { this.click(); };
            var $foo = $(document.getElementById('FormButtonFileBrowserMalsup'));
               //alert($foo);
               $foo.on("click", function() { alert("hhh"); });
      }
  }
  </script>
</head>
<body>
</body>
<form id="web" name="web">
  <input type="button" value="Open Me" onclick="foo();" />
</form>
</body>

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery的触发方法:

$('#id').trigger('click')

您可以在jQuery的文档here

中阅读更多内容