【问题标题】:Get value from Javascript to Livewire Component从 Javascript 获取价值到 Livewire 组件
【发布时间】:2021-06-15 00:59:56
【问题描述】:

能否告诉我是否可以获取 Javascript 字段的值并将其传递给 Laravel Livewire 属性:

首先我用 Javascript 在输入中加载以下值:

document.getElementById('lat-span').value = (place.geometry['location'].lat());

这会使用值加载输入,现在我尝试使用 wire: model 检索该值并且它显示为 null,我知道它没有按照我的想法完成:

<input  wire:model="latitud" value="lat-span" id="lat-span" />

如何直接将此 javascript 值传递给 Livewire 属性?

【问题讨论】:

    标签: javascript laravel laravel-livewire


    【解决方案1】:
    <script>
    // ....
    Livewire.emit('getLatitudeForInput', place.geometry['location'].lat());
    </script>
    

    在组件中

    public $latitud;
    protected $listeners = [
         'getLatitudeForInput'
    ];
    //
    public function getLatitudeForInput($value)
    {
        if(!is_null($value))
            $this->latitud = $value;
    }
    

    光秃秃的

    <input  wire:model="latitud" id="lat-span" />
    

    【讨论】:

    • 这很完美,虽然我对它的功能有疑问,例如我还需要添加长度和其他属性,这里似乎是一种独特的方法或者我可以传递更多的值吗?
    • 我以前不这样做,但我认为你可以在JQuery中构建一个对象或一个数组,然后通过这种方式将它发射到组件中
    • 你好@Prospero 我问你一个问题,它在测试项目中运行良好,但在生产项目中我收到以下错误:Livewire.emit 不是函数。如果您能给我一个建议,我将不胜感激
    【解决方案2】:
    <script>
    @this.set('latitud', place.geometry['location'].lat());
    </script>
    

    在脚本标签内试试这个

    【讨论】:

    • 这样的东西会很完美,但它对我不起作用。基本上这应该用值设置属性?
    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2021-10-24
    相关资源
    最近更新 更多