我正在尝试添加一个js变量作为我的动作参数,其中该变量因每个按钮而异,表格将被创建并将被发送到bootstrap模态体(最初模态体是空白的)。 这是我试图分配给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"
我错过了什么吗?或做错了吗?
答案 0 :(得分:0)
如果我像这样写数据部分就可以了
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"
答案 1 :(得分:0)
您可以在操作中使用占位符,并在发生单击时使用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 :(得分:-1)
你可以这样写
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()}}';