页面上的javascript:
function submitPostForm(url, data) {
var form = document.createElement("form");
form.action = url;
form.method = 'POST';
form.style.display = 'none';
for (var attr in data) {
var param = document.createElement("input");
param.name = attr;
param.value = data[attr];
param.type = 'hidden';
form.appendChild(param);
}
document.body.appendChild(form);
form.submit();
}
发生某些事件后(例如点击“提交”):
var products = jQuery('#spreadSheetWidget').spreadsheet('getProducts');
var postData = {
'action': action,
'products': products
}
submitPostForm(jQuery('#submitURLcreateorder').val(), postData);
在控制器中:
public function bestillingAction(Request $request) {
$products = $request->request->get('products', null);
$action = $request->request->get('action', null);
return $this->render(
'VarelagerBundle:Varelager:bestilling.html.twig',
array(
'postAction' => $action,
'products' => $products
)
);
}
在模板中(在我的情况下为bestilling.html.twig):
{% block resources %}
{{ parent() }}
<script type="text/javascript">
jQuery(function(){
{% autoescape false %}
{% if products %}
jQuery('#spreadSheetWidget').spreadsheet({
enable_listitem_amount: 1,
products: {{products}}
});
jQuery('#spreadSheetWidget').spreadsheet('sumQuantities');
{% endif %}
{% endautoescape %}
});
</script>
{% endblock %}
Alrite,我想这就是您想要的:)
编辑
要发送某些内容而不模拟表单,可以使用jQuery.ajax()。这是一个与上述相同的示例,它不会触发页面刷新。
jQuery.ajax({
url: jQuery('#submitURLsaveorder').val(),
data: postData,
success: function(returnedData, textStatus, jqXHR ){
jQuery('#spreadSheetWidget').spreadsheet('clear');
window.alert("Bestillingen ble lagret");
},
error: jQuery.varelager.ajaxError,
dataType: 'text',
type: 'POST'
});