0

我在 symfony2.0 中有一个使用 twig 的路径函数生成的链接,例如:

<a id="aBtn" href="{{ path("SomeController_someControllerAction",{'section_id':
    section_id, 'period_id': period.getId }) }}>A link</a>

而且我还有一些javascript,每次用户更改页面上的部分时都会更新生成的URL的“section_id”部分:

function updateSectionId(id){
  var aBtn = $("#aBtn");
  var href = aBtn.attr('href');
  var splitted = href.split("/");
  splitted[splitted.length-2] = id; //My routing puts the period_id at that position
  //(Yes i know its pretty hardcoded...)
  aBtn.attr('href',splitted.join("/"));
}

我已经验证并且链接已更新为相应的 section_id。

但是,调试收到此请求的操作我发现它总是收到 section_id = 1。

public function newQuestionDialogAction($period_id, $section_id){
  //$period_id = 1 ALWAYS, regardless of href value on the link.

我真的一无所知......我在树枝的路线生成中遗漏了什么吗?

编辑:这是路线配置

SomeController_someControllerAction:
    pattern: /{period_id}/section/{section_id}/someControllerAction
    defaults: { _controller: "SomeBundle:SomeController:someControllerAction" }
    requirements:
      _method: GET

EDIT2:我有一种预感,这个问题的根源是 jquery-bootstrap 插件“Modal 2”,用于向服务器执行 ajax 请求。如果我发现问题,我会告诉你。谢谢!

https://github.com/Nikku/jquery-bootstrap-scripting

4

2 回答 2

0

所以问题确实出在我的javascript上,解决方案非常简单。我在这里发帖以防有人偶然发现 jquery bootstrap 库存在同样的问题。

一旦你打电话

$(document).controls()

每个元素上的所有“open-dialog”类都被删除,因此无论您调用多少次 $(document).controls() 它都不会使用正确的 href 更新 onClick 事件。解决方法是在再次调用 $(document).controls() 之前添加“open-dialog”类。

$("#some-element").addClass("open-dialog");
$(document).controls();

所以,就我而言,解决方案是更新我的 updateSectionId 方法

function updateSectionId(id){
  var aBtn = $("#aBtn");
  var href = aBtn.attr('href');
  var splitted = href.split("/");
  splitted[splitted.length-2] = id; //My routing puts the period_id at that position
  //(Yes i know its pretty hardcoded...)
  aBtn.attr('href',splitted.join("/"));
  //Add open-dialog class and call controls to reattach the href
  aBtn.addClass("open-dialog");
  $(document).controls();
}
于 2012-11-17T03:47:25.517 回答
0

查看浏览器中的链接。它真的会以正确的 ID 进入新页面吗?如果没有,您的 javascript 有问题。

于 2012-10-23T21:01:20.507 回答