我无法以这种方式发送Ajax请求 为网站找到了合适的表格 https://github.com/codrops/MinimalForm 并尝试发送数据而不刷新页面 我究竟做错了什么?需要像form.submit(){}?
形式
<form id="theForm" class="simform" autocomplete="off">
<div class="simform-inner">
<ol class="questions">
@foreach($answers as $answer)
<li>
<span><label for="q">{{ $answer->answer }}</label></span>
<label class="checkbox" for="checkbox1" style="display:none;">
<input type="checkbox" value="{{$answer->id}}" id="checkbox1" checked name="id" data-toggle="checkbox" >
</label>
<input id="q" name="word" type="text"/>
</li>
@endforeach
</ol><!-- /questions -->
<button class="submit" type="submit">Send answers</button>
</form><!-- /simform -->
JS
<script src="/tests/js/classie.js"></script>
<script src="/tests/js/stepsForm.js"></script>
<script>
var theForm = document.getElementById( 'theForm' );
new stepsForm( theForm, {
onSubmit : function( form ) {
// hide form
classie.addClass( theForm.querySelector( '.simform-inner' ), 'hide' );
var query = "word=" + $("input[name=word]").val() +
"&id=" + $("input[name=id]").val();
$.ajax({
type: "POST",
url : "http://example.com/frontend/tests/elementary-2/check",
data : query,
cache: false,
dataType : "json",
success : function(msg){console.log(msg);
if (msg['msg'] == true){
sweetAlert("Good job!", "some", "success");
}else {
sweetAlert("Oops...", "some", "error");
}
}
});
}
} );
</script>
答案 0 :(得分:0)
在表单中输入false
<form id="theForm" class="simform" autocomplete="off" onsubmit="return false;">
您的ajax实际上正在执行,但默认情况下表单也会发出非ajax请求。 return false将阻止表单以默认行为提交。
答案 1 :(得分:0)
好的,如果您询问有关如何通过ajax提交表单的建议,那么这是一个示例 -
$(document).on("click", ".submit", function () {
$.ajax({
type: "POST",
url: "your/url",
data: {your:data},
success: function(data) {
//and from data parse your json data and show error message in the modal
var obj = $.parseJSON(data);
if(obj!=null)
{
$('#mssg').html(obj['message']);
}
}});
});