Validating a form in Twitter Bootstrap Model

前端 未结 2 628
闹比i
闹比i 2021-01-14 13:50

I have a form in a modal with some required fields, but when I hit submit the form just reloads and I don\'t get any message for validation that I am using for submitting th

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 14:49

    You can add "data-dismiss=modal" to the InfroTextSubmit button, and then use 'return false' on the '#InfroTextSubmit' click function to prevent the form from closing. If the form is valid you call 'submit()'.

    Button code:

    
    

    Click function:

    $('#save').click(function(){
    
        if ($('#myField1').val()==="") {
          // invalid
          $('#myField1').next('.help-inline').show();
          return false;
        }
        else {
          // submit the form here
          $('#InfroText').submit();
          return true;
        }     
    });
    

    There a several different approaches to the validation itself.

    Working on Bootply: http://bootply.com/60244

提交回复
热议问题