我使用下面的表单,我想在Dropdown-List中选择一个选项后阻止整个页面刷新。请注意,我似乎没有表格的明确名称。什么是必要的Ajax命令和脚本?我正在使用Jquery:
//all inputs with name products[] and as value the product id $items = Input::get('products'); foreach($items as $item){ $prodid = Product::find($item); echo $prodid['name']; }
<script type ="text/javascript" src="js/jquery.1.8.3.min.js"></script>
答案 0 :(得分:1)
首先,您阻止页面按preventDefault()
重新加载,然后您将向服务器发送一个帖子请求,并在done
函数中接收您的结果。如果出现任何问题,将执行fail
功能。每次发送请求时都会执行always
功能。
$( "form" ).on( "submit", function( event ) {
//this will prevent the page from reloading
event.preventDefault();
//send a post request to the server
//$(this).serialize() will get all the values within your form
//in this case it only gets the value of your select tag
$.post( "test.php", $( this ).serialize())
.done(function( data ) {
alert( "Data Loaded: " + data );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
});