简体   繁体   English

HTML5验证不会触发

[英]HTML5 validation does not trigger

I will go directly to the problem 我会直接解决这个问题
So here is my form 所以这是我的表格

<form action="<?php echo base_url()?>" method="post" id="formfield">
    <center>
        <label>How much? <small>( Minimum of 100 )</small></label>
        <div class="ui input">
          <input type="number" name="amount" required>
        </div>  
        <br>
        <label>Payment type <small>(<a href="#" data-toggle="modal" data-target="#payment_type"> see all </a>)</small></label>
        <br>
        <div class="ui input">
          <input type="text" name="type" required>
        </div>  
        <br>
        <div class="">
        <input type="submit" class="btn btn-primary" value="Order Now" id="btnsubmit" >      
        </div>                                
    </center>   
</form>    

And here is my Javascript, newbie here. 这是我的Javascript,新手在这里。

<script>
  $(function()
{
  $('#btnsubmit').on('click',function()
  {
    $(this).val('Please wait ...')
      .attr('disabled','disabled');
    $('#formfield').submit();
  });

});
</script>

So the problem is that the form submit without triggering the html5 validation 所以问题是表单提交时没有触发html5验证
I also wanted to add alert if Ok proceed but should trigger the html5 validation 如果确定,我还想添加警报但是应该触发html5验证
Can anyone help me? 谁能帮我? I will really appreciate it. 我真的很感激。 Advance thank you. 谢谢你。

The form is submitting without triggering the HTML5 validation because you're submitting the form in your JavaScript when you call: $('#formfield').submit(); 表单提交时没有触发HTML5验证,因为您在调用时在JavaScript中提交表单: $('#formfield').submit();

To add a confirmation dialog, you could use something as simple as confirm , though confirm is not always the best idea. 要添加确认对话框,您可以使用像confirm一样简单的内容,但confirm并不总是最好的主意。 You could add something like this to your event listener: 你可以在事件监听器中添加这样的东西:

if (confirm('Are you sure?')) {
    // Yes
    $(this).val('Please wait ...').attr('disabled','disabled');
} else {
    // Prevent form from being submitted
    return false;
}

Snippet (doesn't exactly work, because stacksnippets.net doesn't allow forms to be submitted, but check your console) 片段(不完全正常,因为stacksnippets.net不允许提交表单,但请检查您的控制台)

 function go() { if (confirm('are you sure?')) // Form would be submitted, but stacksnippets prevents it. document.querySelector('form').submit(); else return false; } 
 <form action=""> <input type="text" required> <button onclick="go()"> Submit </button> </form> 

May be this will help you; 可能这会对你有所帮助;

 $(document).ready(function() { $('#formfield').on('submit', function(e) { $("#btnsubmit").val('Please wait ...') .attr('disabled', 'disabled'); }); }); 
 <form action="<?php echo base_url()?>" method="post" id="formfield"> <center> <label>How much? <small>( Minimum of 100 )</small> </label> <div class="ui input"> <input type="number" name="amount" required> </div> <br> <label>Payment type <small>(<a href="#" data-toggle="modal" data-target="#payment_type"> see all </a>)</small> </label> <br> <div class="ui input"> <input type="text" name="type" required> </div> <br> <div class=""> <input type="submit" class="btn btn-primary" value="Order Now" id="btnsubmit"> </div> </center> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM