【问题标题】:Trigger the html file-browser click event in javascript/jquery? [duplicate]在 javascript/jquery 中触发 html 文件浏览器点击事件? [复制]
【发布时间】:2014-04-19 22:22:11
【问题描述】:

那么这个脚本有什么问题呢?我正在尝试触发不起作用的文件浏览器 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>

【问题讨论】:

  • 如果我们从未听说过 JQuery 的触发器,为什么您将其标记为重复和负分? 摇头

标签: javascript jquery


【解决方案1】:

你可以像这样使用jQuery的触发方法:

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

您可以在 jQuery 的文档here中阅读更多内容

【讨论】:

  • 谢谢.. 我从没听说过触发器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
相关资源
最近更新 更多