e.preventDefault不阻止提交

时间:2017-04-27 15:40:19

标签: javascript jquery

所以在localhost上我根本没有这个功能的问题,它完美无缺,但现在我把它上传到我的主机,它似乎不再起作用了。表单有点提交,URL从其中获取数据字符串中的所有信息。请帮我解决。

这会添加到地址栏中的网址:

  ?

inlineRadioOptions =预先设定的&安培; customname =安培; customtype =安培;犯罪=安培; DescriptionForm =安培; inlineRadioOptions3 =无&安培;报告=安培;所选文本= 1&安培; CordX = 4675&安培;科迪= 4558&安培; SubmitCrime =

$("#CrimeForm").submit(function(e) {
  e.preventDefault();
  if ($('#RadioCrime2').is(':checked')) {
    var Crime = $("#customname").val();
    var Type = $("#customtype").val();
  } else {
    var Crime = $("#select2").val();
    var Type = 'NONE';
  }

  if ($('#RadioDiv2').is(':checked')) {
    var UseDiv = 1;
  } else {
    var UseDiv = 0;
  }

  var value = $(this).find('select[name="DivisionSelect"]').val();
  var divss = (value ? value.join('') : '');

  var DescriptionForm = $("#DescriptionForm").val();
  var Report = $("#Report").val();
  var PinID = $("#selected-text").val();
  var CordX = $("#CordXNew").val();
  var CordY = $("#CordYNew").val();
  var UserID = <?php echo $AccountID;?>;
  var dataString = 'Crime=' + Crime + '&Type=' + Type + '&DescriptionForm=' + DescriptionForm + '&PinID=' + PinID + '&CordX=' + CordX + '&CordY=' + CordY + '&UserID=' + UserID + '&DivSelect=' + UseDiv + '&Divs=' + divss + '&Report=' + Report;
  if (Crime == '' || Type == '' || DescriptionForm == '' || PinID == '' || CordX == '' || CordY == '') {
    document.getElementById("Alert-group").innerHTML = '<div class="alert alert-danger" role="alert">You need to fill out everything!</div>';
  } else {
    var n = DescriptionForm.length;
    if (n > 500) {
      document.getElementById("Alert-group").innerHTML = '<div class="alert alert-danger" role="alert">You have ' + n + ' characters in the description, limit them to 500!</div>';
    } else {
      $.ajax({
        type: "POST",
        url: "SendInput.php",
        data: dataString,
        cache: false,
        success: function(result) {
          document.getElementById("Alert-group").innerHTML = '<div class="alert alert-success" role="alert">' + result + '</div>';
          $('#DescriptionForm').val('');
          $('#customname').val('');
          $('#Report').val('');
          $('#customtype').prop('selectedIndex', 0);
          $("#select2").val(null).trigger("change");
          document.getElementById("CustomCrime").style.display = "none";
          document.getElementById("DefaultCrime").style.display = "block";
          document.getElementById("RadioCrime").checked = true;
          document.getElementById("RadioDiv").checked = true;
          document.getElementById("ChooseDivision").style.display = "none";
          $("#DivisionSelect option:selected").prop("selected", false);
        }
      });
    }
  }
  return false;
});

1 个答案:

答案 0 :(得分:1)

您可以在表单中使用常规按钮,而不是依赖表单提交及其事件取消。 按钮有点像:<input type='button' id='btnSendCrimeForm' value='Send Form' />

然后,在document.ready回调的内容中,您可以注册上述按钮的click事件,在该事件中执行您希望在表单提交时执行的代码。

$("#btnSendCrimeForm").click(function(e) {
    if($('#RadioCrime2').is(':checked')) {
        var Crime = $("#customname").val();
        var Type = $("#customtype").val();
    }else{
        var Crime = $("#select2").val();
        var Type = 'NONE';
    }

    if($('#RadioDiv2').is(':checked')) {
        var UseDiv = 1;
    }else{
        var UseDiv = 0;
    }

    var value = $(this).find('select[name="DivisionSelect"]').val();
    var divss = (value ? value.join('') : '');

    var DescriptionForm = $("#DescriptionForm").val();
    var Report = $("#Report").val();
    var PinID = $("#selected-text").val();
    var CordX = $("#CordXNew").val();
    var CordY = $("#CordYNew").val();
    var UserID = <?php echo $AccountID;?>;
    var dataString = 'Crime='+ Crime + '&Type='+ Type + '&DescriptionForm='+ DescriptionForm +'&PinID='+ PinID + '&CordX='+ CordX + '&CordY='+ CordY + '&UserID='+ UserID + '&DivSelect='+ UseDiv + '&Divs='+ divss + '&Report='+ Report;
    if(Crime==''||Type==''||DescriptionForm==''||PinID==''||CordX==''||CordY=='')
    {
        document.getElementById("Alert-group").innerHTML = '<div class="alert alert-danger" role="alert">You need to fill out everything!</div>';
    }
    else
    {
        var n = DescriptionForm.length;
        if(n > 500){
            document.getElementById("Alert-group").innerHTML = '<div class="alert alert-danger" role="alert">You have '+ n +' characters in the description, limit them to 500!</div>';
        }else{
            $.ajax({
                type: "POST",
                url: "SendInput.php",
                data: dataString,
                cache: false,
                success: function(result){
                    document.getElementById("Alert-group").innerHTML = '<div class="alert alert-success" role="alert">'+result+'</div>';
                    $('#DescriptionForm').val('');
                    $('#customname').val('');
                    $('#Report').val('');
                    $('#customtype').prop('selectedIndex',0);
                    $("#select2").val(null).trigger("change"); 
                    document.getElementById("CustomCrime").style.display = "none";
                    document.getElementById("DefaultCrime").style.display = "block";
                    document.getElementById("RadioCrime").checked = true;
                    document.getElementById("RadioDiv").checked = true;
                    document.getElementById("ChooseDivision").style.display = "none";
                    $("#DivisionSelect option:selected").prop("selected", false);
                }   
            });
        }
    }
    return false;
});