【问题标题】:jquery's submit method not submitting the input type submit form datajquery的提交方法没有提交输入类型提交表单数据
【发布时间】:2019-01-28 10:43:00
【问题描述】:

我正在尝试使用两个提交按钮提交表单。这是 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 调用检查验证码,并在成功验证码检查时提交表单。但是在action页面中,我无法转发这个Submit-type-1或demo-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 点击时获得process_upload.php 页面上$_POST["Submit-type-1"] 的值。所以任何人都可以帮助我理解这背后的原因并解决这个问题。

【问题讨论】:

  • 试试这个来选择你的元素$('input:submit[name="Submit-type-1"]')
  • 请提供html表格
  • @Ash:添加了html表单
  • 如果您无法获得提交帖子变量,为什么不将其添加到您的数据中? - data: {captcha_code: captcha, 'Submit-type-1': 'Submit' }不通过它就不会神奇地出现
  • @Pete 这样,我可以在“captcha_check.php”页面中获取数据。但不在process_upload.php 页面中。我想我的问题并不清楚。所以我添加了页面名称。

标签: jquery ajax


【解决方案1】:
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(){});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多