我正在使用 FOSJsRoutingBundle 在 JavaScript 文件中生成 Symfony 2 路由 URL。
我已经按照文档安装了这个包(https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/tree/master/Resources/doc)
尝试使用 ajax 脚本实现表单提交,用户可以在其中评分和评论。
这是我的 Twig /html代码:
<form id="feedback" method="post" name="feedback">
<div class="form">
<div class="form_layer">
<div class="rating">
<label>Rate Us:</label>
<input type="radio" name="rating" value="1"><small>One</small>
<input type="radio" name="rating" value="2"><small>Two</small>
<input type="radio" name="rating" value="3"><small>Three</small>
<input type="radio" name="rating" value="4"><small>Four</small>
<input type="radio" name="rating" value="5"><small>Five</small>
</div>
</div>
<div class="form_layer floated1">
<div class="comment">
<label>Please give your feedback</label>
<textarea rows="5" cols="32" id="comment" name="feedback"></textarea>
</div>
<div class="clr"></div>
</div>
<span class="comment_btn"><input type="submit" class="comment_submit" name="validate" value="Submit"></span>
</div>
</form>
这是我的 JavaScript 代码:
//Ajax Form Submission
$(function() {
$('.comment_submit').click(function() {
var rating = $(':radio').val();
var comment = $('#comment').val();
alert("comment: "+comment);
var route = Routing.generate('user_feedback');
alert("Route: "+route);
/* $.ajax({
type: "POST",
url: Routing.generate('user_feedback', { "rating": rating, "comment": comment}),
success: function() {
$('div.middle').html("<div class='message'></div>");
$('.message').html("<h3>Thank You!</h3>")
.append("<p>Your comment has been submitted</p>")
.hide()
.fadeIn(1500);
}
});
return false; */
//alert("Rating"+comment);
//alert("Feedback Submited");
});
});
我也在我的 routing.yml 文件中添加了路由 url
现在的问题是当我提交表单时,我无法获取路由值。我试图提醒您在代码中看到的路线值,但我没有得到值(value)。当我提交时,表单只会刷新。
代码有问题吗?
我是 Symfony 2 的新手,所以请帮助我。
谢谢你
最佳答案
它不是一个 symfony 问题,问题在于 javascript
您有一个提交按钮,它将提交您的表单。为防止表单提交,您必须通过以下方式更改点击功能:
$('.comment_submit').click(function(e) {
e.preventDefault();
// your code goes here
return false;
}
关于jquery - FOSJsRoutingBundle : Cannot Generate routing url inside javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8922803/