我有问题从刀片模板传递数据到功能点击。我不想使用onclik。我有一个全局变量传递给我的刀片tempate像:oreder和blick on button后我需要传递这些数据。 HEre是我的代码:
<button class="btn" id="someButton">Some text</button>
这里是jQuery:
$(document).ready(function () {
$('#someButton').on('click', function (here I need to pass global data from blade like : order->id etc) {});
})
有可能这样做吗?
答案 0 :(得分:2)
HTML:
<button class="btn" id="someButton" data-order-id="{{$order->id}}">Some text</button>
JQuery的:
$('#someButton').on('click', function() {
var order_id = $(this).data('order-id');
});
答案 1 :(得分:0)
如果您要传递的变量是全局的并且适用于整个页面,我会将其放在隐藏的输入标记中。然后在需要时检索该值。
<input name=“globalValue” value=“{{ $value }}” type=“hidden”></input>
...
$(‘#someButton’).click(function(e) {
var globalValue = $(‘input[name=“globalValue”’).val();
}
如果该值特定于按钮,则在按钮上放置data-
属性。
<button id=“someButton” data-value=“{{ $value }}”>Some Button</button>
...
$(‘#someButton’).click(function(e) {
var buttonValue = $(‘#someButton’).data(‘value’);
}
答案 2 :(得分:0)
如果你想在你的jQuery函数中传递全局数据,那么你可以在隐藏字段中获取该gloabal变量,如
<input type="hidden" id="gloabalVariable" value="{{$order->id }}">
在你的jQuery中,你可以通过以下行获得它:
var orderId = $('#gloabalVariable').val();
你也提到过你不想使用onClick事件。
在这种情况下,您可以在按钮上绑定.click()
或.mousedown()
事件。
如果您需要进一步的帮助,请告知我们。
谢谢