0

我正在尝试禁用此脚本中的表单操作。有人可以帮我禁用或删除表单“动作”变量吗?我正在使用脚本编写表单来上传图像,但是当我提交表单时,会调用在此脚本上设置的操作。

有人可以请教吗?

  function $m(theVar){
 return document.getElementById(theVar)
 }
  function remove(theVar){
  var theParent = theVar.parentNode;
  theParent.removeChild(theVar);
  }
  function addEvent(obj, evType, fn){
  if(obj.addEventListener)
    obj.addEventListener(evType, fn, true)
if(obj.attachEvent)
    obj.attachEvent("on"+evType, fn)
  }
  function removeEvent(obj, type, fn){
if(obj.detachEvent){
    obj.detachEvent('on'+type, fn);
}else{
    obj.removeEventListener(type, fn, false);
}
  }

   // browser detection
  function isWebKit(){
return RegExp(" AppleWebKit/").test(navigator.userAgent);
   }

   // send data
  function ajaxUpload(form){
var detectWebKit = isWebKit();
var get_url = 'upload.php';// php file
var div_id = 'upload_area';// div id where to show uploaded image
var show_loading = '<img src="img/loading.gif" />';// loading image
var html_error = '<img src="img/error.png" />';// error image

// create iframe
var sendForm = document.createElement("iframe");
sendForm.setAttribute("id","uploadform-temp");
sendForm.setAttribute("name","uploadform-temp");
sendForm.setAttribute("width","0");
sendForm.setAttribute("height","0");
sendForm.setAttribute("border","0");
sendForm.setAttribute("style","width: 0; height: 0; border: none;");

// add to document
form.parentNode.appendChild(sendForm);
window.frames['uploadform-temp'].name="uploadform-temp";


 // add event
  var doUpload = function(){
    removeEvent($m('uploadform-temp'),"load", doUpload);
    var cross = "javascript: ";
cross += "window.parent.$m('"+div_id+"').innerHTML =        document.body.innerHTML;   void(0);";
    $m(div_id).innerHTML = html_error;
    $m('uploadform-temp').src = cross;
    if(detectWebKit){
        remove($m('uploadform-temp'));
    }else{
        setTimeout(function(){ remove($m('uploadform-temp'))}, 250);
    }
   }
addEvent($m('uploadform-temp'),"load", doUpload);

  // form proprietes
form.setAttribute("target","uploadform-temp");
form.setAttribute("method","post");
  form.setAttribute("action",get_url);
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");

// loading
if(show_loading.length > 0){
    $m(div_id).innerHTML = show_loading;
}
  // submit
form.submit();
  return true;
  }
4

2 回答 2

0

将您的函数挂在onsubmit表单return false;的最后 - 这将取消标准表单提交。

于 2013-09-15T00:49:44.893 回答
0
form.submit(evt);
    evt.preventDefault();
}

根据jQuery.submit()文档,还将绕过实际的表单提交过程:

现在,当提交表单时,会提醒消息。这发生在实际提交之前,因此我们可以通过调用.preventDefault()事件对象或从我们的处理程序返回 false 来取消提交操作。

于 2013-09-15T00:57:03.123 回答