【问题标题】:How to add a javascript variable as action parameter in Laravel form如何在 Laravel 表单中添加一个 javascript 变量作为动作参数
【发布时间】:2018-04-15 00:18:33
【问题描述】:

我正在尝试添加一个 js 变量作为我的操作参数,其中该变量因每个按钮而异,一个表单将被创建并将被发送到引导程序模态体(最初模态体是空白的)。 这是我试图分配给 modal-body html 的 js 代码

$('.postComment').click(function(e){
    e.preventDefault;
    var id = $(this).attr('id'); //this is the id suppose:id='1234abcd'

    var data =  '{{Form::open(array("method" => "POST","route" => ["post.comment",'+id+'],"class" => "form-horizontal"))}}'+
                '{{Form::textarea("comment", null, array("required","class"=>"form-control","placeholder"=>"Add a comment"))}}'+
                '{{Form::submit("Comment",array("class"=>"btn btn-info"))}}'+
                '{{Form::close()}}';
    $('#comment').modal();
    $('#comment').on('shown.bs.modal', function(){
        $('#comment .modal-body').html(data);
    });
    $("#comment").on('hidden.bs.modal', function(){
            $('#comment .modal-body').html('');
        });
});

当点击表单中的评论按钮时,会出现这样的动作

action="/post/comment/+id+"

但预期是

action="/post/comment/1234abcd"

我错过了什么吗?还是做错了?

【问题讨论】:

  • 您不能混合使用 php 方法和 javascript 变量。 php 在页面加载之前很久就在服务器上运行,而 javascript 在页面加载后在浏览器中运行
  • 但是如果我使用普通的 html 表单它就可以了

标签: javascript jquery laravel-5 bootstrap-modal


【解决方案1】:

您可以在操作中使用占位符,并在点击发生时使用 javascript 替换它。

您尝试做的问题是 laravel 表单代码在 javascript 在浏览器中运行之前很久就在服务器上运行

试试

 var data =  '{{Form::open(array("method" => "POST","route" => ["post.comment",'+id+'],"class" => "form-horizontal"))}}'+
                '{{Form::textarea("comment", null, array("required","class"=>"form-control","placeholder"=>"Add a comment"))}}'+
                '{{Form::submit("Comment",array("class"=>"btn btn-info"))}}'+
                '{{Form::close()}}';

var $form = $(data);
$form.attr('action', function(_, action){
  return action.replace('+id+', id)
});

$('#comment').modal();
$('#comment').on('shown.bs.modal', function(){
    $('#comment .modal-body').html($form);
});

【讨论】:

    【解决方案2】:

    如果我像这样写 data 部分,它会起作用

    var data =  '<form method="post" class="form-horizontal" action="post/comment/'+id+'">'+
                '<input type="hidden" name="_token" value="{{ csrf_token() }}">'+
                '<textarea id="commentBox" name="comment" class="form-control" required></textarea>'+
                '<input type="submit" name="Comment" value="Comment" class="btn btn-info"/>'+
                '</form>';
    

    在它显示的表单动作中

    action="/post/comment/1234abcd"
    

    【讨论】:

      【解决方案3】:

      你可以这样写

         var id123 = $(this).attr('id'); //this is the id suppose:id='1234abcd'
      
          var data =  '{{Form::open(array("method" => "POST","route" => ["post.comment'+id123+'"],"class" => "form-horizontal"))}}'+
                      '{{Form::textarea("comment", null, array("required","class"=>"form-control","placeholder"=>"Add a comment"))}}'+
                      '{{Form::submit("Comment",array("class"=>"btn btn-info"))}}'+
                      '{{Form::close()}}';
      

      【讨论】:

      • 我现在试试
      • 这行不通。 id 在编译 php 时不存在
      • 不,它会抛出一个 ErrorException,因为这个 id 没有定义,但我之前定义了它。
      • 你把id属性放在你的按钮标签里了吗?
      • 我使用普通的 html 表单而不是刀片表单解决了它
      猜你喜欢
      • 2017-02-09
      • 2016-10-11
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2011-03-29
      • 2016-04-27
      • 1970-01-01
      • 2015-05-13
      相关资源
      最近更新 更多