简体   繁体   English

如何在JS(PHP)中使用Laravel Blade

[英]How to use Laravel Blade in JS (PHP)

I am developing a blog project in Laravel 5.6. 我正在Laravel 5.6中开发一个博客项目。 I am using the Laravel built-in function called str_slug() to convert titles into slugs and JavaScript to auto generate the slugs using JavaScript's keyup function. 我正在使用Laravel内置函数str_slug()将标题转换为str_slug()并使用JavaScript使用JavaScript的keyup函数自动生成str_slug() Here's my code: 这是我的代码:

var postTitle = $('#post-title');
var postSlug = $('#post-slug');
postTitle.keyup(function() {
    postSlug.val({{ str_slug(postTitle.val()) }});
});

However, it's throwing an error of unknown constant. 但是,它将引发未知常量的错误。 Can anyone tell me how to what I want? 谁能告诉我我想要什么? I am new to Laravel. 我是Laravel的新手。

Here's a simple function to create a slug in JS: 这是一个在JS中创建子弹的简单函数:

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
}

Source Gist 来源要点

You can use the html markup to get this done. 您可以使用html标记来完成此操作。 In the html mark up create a hidden input and put in the value of str_str() operation on the post-title and in your js set the post-slug input value to that on keyup. 在html标记中,创建一个隐藏的输入,并将str_str()上的str_str()操作的值str_str() ,在您的js中,将str_str()后的输入值设置为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());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM