Javascript 如何在JS(PHP)中使用Laravel刀片
我正在用Laravel 5.6开发一个博客项目。我正在使用名为Javascript 如何在JS(PHP)中使用Laravel刀片,javascript,php,jquery,laravel,laravel-blade,Javascript,Php,Jquery,Laravel,Laravel Blade,我正在用Laravel 5.6开发一个博客项目。我正在使用名为str_slug()的Laravel内置函数将标题转换为slug,并使用JavaScript的keyup函数自动生成slug。这是我的密码: var postTitle = $('#post-title'); var postSlug = $('#post-slug'); postTitle.keyup(function() { postSlug.val({{ str_slug(postTitle.val()) }}); })
str_slug()
的Laravel内置函数将标题转换为slug,并使用JavaScript的keyup
函数自动生成slug。这是我的密码:
var postTitle = $('#post-title');
var postSlug = $('#post-slug');
postTitle.keyup(function() {
postSlug.val({{ str_slug(postTitle.val()) }});
});
然而,它抛出了一个未知常数的错误。有人能告诉我怎么做我想要的吗?我是拉威尔的新手。您可以使用html标记来完成这项工作。在html标记中,创建一个隐藏输入,并在帖子标题上输入
str\u str()
操作的值,在js中,将帖子slug输入值设置为keyup上的值
<input type="text" id="post-title" value="{{$title}}">
<input type="hidden" id="hidden-post-title" value="{{str_slug($title)}}">
var postTitle = $('#post-title');
var postSlug = $('#post-slug');
postTitle.keyup(function() {
postSlug.val($('#hidden-post-title').val());
});
var postTitle=$(“#post title”);
var postslaug=$(“#post slug”);
postTitle.keyup(函数(){
val($('#隐藏的文章标题').val());
});
这里有一个简单的函数可以在JS中创建slug:
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
这是行不通的,
str_slug
是一个PHP函数,在发送到客户端之前,在服务器上对其进行评估。提出Ajax请求或使用javascript slug generator是您的两个最佳选择。您能给我介绍一个JS助手函数,它可以在slug中转换标题吗!