2017-11-03 187 views
0

我为我的应用程序使用了Laravel。Laravel:将复选框的值发送给控制器而无需发布?

我在我的HTML页面上打了一个PRINT按钮,它只是简单地调用一个路径,以便能够通过DOMPDF将它打印到PDF。现在

<a href="{{ route('print_overzicht_facturen') }}" class="btn btn-default">Print</a> 

,在我的控制,我想这样

<div class="col-lg-7 selectie"> 
    <input type="radio" name="factuur_selectie" id="factuur_selectie" value="1" checked> Alle facturen&nbsp 
    <input type="radio" name="factuur_selectie" id="factuur_selectie" value="2"> Betaalde facturen&nbsp; 
    <input type="radio" name="factuur_selectie" id="factuur_selectie" value="3"> Onbetaalde facturen 
</div> 

在我的控制,我不能找到办法让已在HTML中创建一个单选按钮的值获取复选框的价值,我想因为我没有提交?

我该如何能够获得单选按钮的价值?

public function printFacturen(Request $request){ 

} 

我已经尝试以下三种方式,但它不工作:

$fields = Input::get('factuur_selectie');     
$value = $request->get('factuur_selectie'); 
$request->input('factuur_selectie'); 

Bestregards,

戴维

+0

您必须将其发送到服务器。 JavaScript或表单。无法避免它。 – Ohgodwhy

+0

您的路线如何定义? – Camilo

+0

如果我的答案解决了您的问题,请考虑将其标记为已接受。 – Camilo

回答

1

则需要使用JavaScript抢到factuur_selectie值,并添加它到生成的URL,如:

var val = document.querySelector('#factuur_selectie:checked').value; 

var btn = document.querySelector('.btn.btn-default'); 

var url = btn.getAttribute('href'); 

btn.setAttribute('href', url + '?factuur_selectie=' + val); 

然后,您应该能够从您的控制器检索factuur_selectie

可能您需要在每次选择某个选项时更新该值。在这种情况下,你可以从那么event本身的价值:

var btn = document.querySelector('.btn.btn-default'); 

document.querySelector('.selectie').addEventListener('change', function(event) { 

    var val = event.target.value; 

    var url = btn.getAttribute('href'); 

    var pos = url.indexOf('?'); 

    // If URL already contains parameters 
    if(pos >= 0) { 

    // Remove them 
    url = url.substring(0, pos); 
    } 

    btn.setAttribute('href', url + '?factuur_selectie=' + val); 
}); 

这里有一个working example

+1

不要忘记改变路线呼叫。 正在使用get方法传递参数。 –