如何让jQuery将表单提交到帖子

时间:2017-07-09 04:08:16

标签: javascript jquery

我有一个用jQuery验证的表单。

<form id="target" class="center-block" action="/" method="POST">
    <textarea class="form-control" id="name" rows="3" name="name" placeholder="Enter a burger you would like to devour!"></textarea>
    <button type="button" id="submit" class="center-block btn btn-default top">Submit</button>
</form>

验证后,我也尝试通过jQuery提交表单。

if (validateForm() == true) {
   $("#target").submit();
}

验证工作正常,但表格似乎没有提交到后期路线。我能够使用ajax发布表单,但是帖子在完成后不会重定向。希望这种方法能产生预期的效果。

该应用程序在Express上运行并使用MySQL。

3 个答案:

答案 0 :(得分:0)

使用Ajax如果您想重定向到当前页面,请在成功时重新加载当前页面

$.ajax({    
    url: yourUrl,
    type: "post",
    data: $("#target").serialize(),
    success: function(r){
        //form posted successfully
        window.location.reload();
    },
    error: function(error){
      alert(error);
    }
});

答案 1 :(得分:0)

我可能会有一个错字,但想法是:

$('#target').submit(function(event){
    event.preventDefault();
}).validate({
    rules: { ... },
    submitHandler: function(form){
        $.ajax{
            type: 'POST',
            data: $(form).serialize(),
            url: '/',
            success: function(result){
                console.log(result);
            }
            error: function(err){
                console.log(err.message);
            }
        }
    });
});

答案 2 :(得分:0)

我不认为您的jquery loss = tf.reduce_mean(tf.nn.sampled_softmax_loss( weights = softmax_weights, biases = softmax_biases, inputs = tf.reduce_sum(embed, 1), labels = train_labels, num_sampled= num_sampled, num_classes= vocabulary_size)) 会起作用,因为元素submit将替换id or name submit方法,您应该只更改id(名称也不应该给出)

form.submit