简体   繁体   English

使用带有textarea和验证的jQuery提交HTML表单

[英]Submit an html form using jQuery with textarea and validation

I have the following html 我有以下HTML

<form  action="" class="form-horizontal" id="submitForm">
  <input class="required input-xxlarge" type="text" placeholder="Please enter a suitable title" name="title">
  <br><br>
  <div id='container' class="well">
    <br>
    <textarea class="span8 required" id='mytextarea' wrap='on'>
      Please enter some value
    </textarea>
    <br>
    <input class="btn btn-info" type="submit" id="submit-button" value="Submit">
  </div>

</form>

When the user clicks submit, I want to send the textarea data as well as the text data to a url. 当用户单击“提交”时,我想将文本区域数据以及文本数据发送到url。 I am also using the validation plugin , just to ensure client side verification. 我也在使用验证插件 ,只是为了确保客户端验证。

And the following is my jQuery: 以下是我的jQuery:

 $(function() {
    $("#submitForm").submit(function(){
        if ($("#submitForm").validate()){
            $.post("/create/post/",
                   {'data':$('#mytextarea').val()},
                   function(data){
                       console.log(data);
                       alert(data);
                   });
        }
    });
    });

As of now it does not seem to work, what is wrong? 到目前为止,它似乎不起作用,这是什么问题? How can I override the submit button, perform validation and then post the data to the server? 如何覆盖提交按钮,执行验证,然后将数据发布到服务器?

validate() is just the initialization method for the plugin, not the one supposed to be used to check if the form is valid. validate()只是插件的初始化方法,而不是用于检查表单是否有效的初始化方法。 Your provided code always "validates" to true, because validate() returns the validation object. 您提供的代码始终“验证”为true,因为validate()返回验证对象。 The actual check to see whether the form is valid is what valid() is for. 用来检查表单是否有效的实际检查是valid()的作用。

In addition, as Demao wrote , you need to stop the event propagation so the form is submitted with ajax. 此外,正如Demao所写 ,您需要停止事件传播,以便使用ajax提交表单。 One way is by returning false at the end of the callback. 一种方法是在回调结束时返回false。

So your code should be something like: 因此,您的代码应类似于:

$(function() {
    $("#submitForm").validate();

    $("#submitForm").submit(function(){                       
        if ($("#submitForm").valid()){
            $.post("/create/post/",
                   {'data':$('#mytextarea').val()},
                   function(data){
                       console.log(data);
                       alert(data);
                   });
        }
        return false;
    });
 });​

Here is a demo: http://jsfiddle.net/hzcF7/ 这是一个演示: http : //jsfiddle.net/hzcF7/

Another way to do this would be to let the plugin do the validation itself and just tell it what to do if the form is valid, using the submitHandler option when you initialize the validation object. 这样做的另一种方法是让插件自己进行验证,并在初始化验证对象时使用submitHandler选项仅告诉它如果表单有效。 Like this: 像这样:

$(function() {
    $("#submitForm").validate({
        submitHandler: function() {
            $.post("/create/post/",
               {'data':$('#mytextarea').val()},
               function(data){
                   console.log(data);
                   alert(data);
               });
        }
    });
 });​​​

Demo 演示

You just need to return false or do an event stop propagation. 您只需要返回false或使事件停止传播即可。 Right now the function continues the normal process of submitting a form after it starts the AJAX. 现在,该功能在启动AJAX之后将继续提交表单的正常过程。 You just need to tell it to stop 您只需要告诉它停止

$(function() {
    $("#submitForm").submit(function(){
        if ($("#submitForm").validate()){
            $.post("/create/post/",
                   {'data':$('#mytextarea').val()},
                   function(data){
                       console.log(data);
                       alert(data);
                   });
        }
        return false;
    });
    });

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

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