在HTML响应中使用碳
在下面的代码中,我想显示数据更新后几个小时前的更新,但是 ' + $item.updated_at->diffInHours(now()) + '
给出以下错误 未捕获的语法错误:预期表达式,得到 '>' 它指向 -> 任何建议表示
赞赏,谢谢
index.blade.php
<script>
...
function fetchAllNames() {
var _url = '{{ route("names.fetch.route") }}';
$.ajax({
type: "GET",
url: _url,
dataType: "json",
success: function(response) {
$('tbody').html("");
$.each(response.name, function($key, $item) {
$('tbody').append('<tr>\
<td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
<td>' + $key + '</td>\
<td>' + $item.name + '</td>\
<td><label id="timeLabel">' + $item.updated_at->diffInHours(now()) + '</label></td>\
\</tr>');
});
}
});
}
...
</script>
CustomController.php
public function FetchName()
{
$name = CandidateName::all();
return response()->json(['name'=>$name]);
}
In the below code, I want to show updated some hours ago since the data is updated, but ' + $item.updated_at->diffInHours(now()) + '
giving following error Uncaught SyntaxError: expected expression, got '>' It is pointing at -> operator
Any Suggestion is appreciated, Thanks
index.blade.php
<script>
...
function fetchAllNames() {
var _url = '{{ route("names.fetch.route") }}';
$.ajax({
type: "GET",
url: _url,
dataType: "json",
success: function(response) {
$('tbody').html("");
$.each(response.name, function($key, $item) {
$('tbody').append('<tr>\
<td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
<td>' + $key + '</td>\
<td>' + $item.name + '</td>\
<td><label id="timeLabel">' + $item.updated_at->diffInHours(now()) + '</label></td>\
\</tr>');
});
}
});
}
...
</script>
CustomController.php
public function FetchName()
{
$name = CandidateName::all();
return response()->json(['name'=>$name]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 $item.updated_at 是一个带有日期时间的字符串,例如“2022-04-01 12:12:00”。
您可以定义一个 javascript 函数
,然后在生成表行时可以调用
虽然它是有效的并且有时很有用,但您可能不想使用 $ 来启动您的 javascript 变量名称($key 和 $item)。这将有助于消除调用 php 函数的诱惑。
Assuming $item.updated_at is a string with a datetime such as '2022-04-01 12:12:00'.
You can define a javascript function
and then when generating your table row you can call
Although it's valid and can be useful on occasion, you probably don't want to use $ to start your javascript variable names ($key and $item). This will help to remove the temptation to call php functions.
您不能在对Ajax的响应中使用PHP,
您有两个选择是否在PHP中处理
在返回响应或使用之前可以通过JS处理它,
另外,请检查此 moment.js库其JavaScript库操纵日期的Javascript库,例如
carbon
library应该应该像那样
在渲染中你html
you cant use PHP in the response to ajax,
you have two options whether to handle it in PHP
before return response or use can handle it via js,
also please check this moment.js Library its Javascript library that manipulates dates like
Carbon
libraryshould be something like that
at render you html