jQuery的submit方法不提交输入类型提交表单数据

时间:2019-01-28 10:43:01

标签: jquery ajax

我正在尝试使用两个提交按钮来提交表单。这是HTML代码:

<form enctype="multipart/form-data" id ="type-1" accept-charset="UTF-8" action="process_upload.php" method="post" onsubmit="return Genome_upload_validation()" >

  <label for="organism">Organism Name</label><label id ="ErrorInput_1_1" ></label>
   <div class="form-group">
   <div class="form-line">
    <input id="organism_1" name ="organism" class="form-control" placeholder="Enter your Organism Name" type="text">
  </div>
 </div>

<label for="verion">Organism Version</label> 
<label id ="ErrorInput_1_2" ></label>
<div class="form-group">
 <div class="form-line">
  <input id="org_version_1" name="version" class="form-control" placeholder="Version" type="text">
 </div>
</div>

<div class="form-group">
 <label for="exampleInputFile">Genbank File</label>
 <input type="file" class="form-control-file" id="genbank_file" name= "genbank_file" aria-describedby="fileHelp">
 <small id="fileHelp" class="form-text text-muted">Please provide genbank file of your genome</small>
 <label id ="ErrorInput1" ></label>
</div>

<img id="captcha1" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
<input type="text" id= "captcha_code1" name="captcha_code1" size="10" maxlength="6" />
<a href="#" onclick="document.getElementById('captcha1').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>
<label id ="ErrorCaptcha1" ></label>
<hr>

<input type="Submit" name = "Submit-type-1" id = "Submit-type-1" Value ="Submit" class="btn btn-primary m-t-15 waves-effect">
<input type="Submit" name = "demo-type-1" id= "demo-type-1"  Value ="Load Demo Data" class="btn btn-primary m-t-15 waves-effect">
<input type="button" name = "down-type-1" Value ="Download Demo Data" class="btn btn-primary m-t-15 waves-effect" onclick="location.href='download.php?type=1'">
</form>

在两种情况下,我都想验证其验证码。我正在使用Securimage模块的验证码。我正在使用ajax调用来检查验证码,并在成功进行验证码检查时提交表格。但是在操作页面中,我无法转发此Submit-type-1demo-type-1,因此无法根据按钮名称编写函数。 这是我要检查的代码:

$('input[name="Submit-type-1"]').click(function(){

    var captcha = $("#captcha_code1").val();
    jQuery.ajax({
        url: 'resources/qform/captcha_check.php',
        type: 'POST',
        data: {captcha_code: captcha},
        dataType: 'json'
    }).done(function(data) {
        if (data.error === 0){
            alert('submit successful');
            $('form#type-1').submit();
        }else{
            alert("There was an error with your submission.\n\n" + data.message);
            jQuery('#captcha_code1').val('');

        }
    });
    return false;
});

基本上,在发布方法页面中,单击 Submit-type-1 单击时,无法获得$_POST["Submit-type-1"]页面上process_upload.php的值。因此,任何人都可以帮助我理解其背后的原因并解决问题。

1 个答案:

答案 0 :(得分:0)

When you create html form with type="submit" then by default onSubmit function get execute.

Here you can use type="button" instead of type="Submit" 
or 
("#Submit-type-1").on('submit', function (e) {}); event.
or 
use e.preventDefault(); first line in $('input[name="Submit-type-1"]').click(function(){});