【问题标题】:pass variable into string in symfony and jquery在 symfony 和 jquery 中将变量传递给字符串
【发布时间】:2015-10-07 13:55:46
【问题描述】:

我想将 id 传递给字符串,但总是出错

  var c = document.getElementById("predefinedMessage");
    var id= c.selectedIndex;
    var text= "{{  form.predefinedMessage.vars.choices[id].data.message }}";

    $('message').val(text);

});

如何在文本变量中传递 id

【问题讨论】:

  • 你遇到了什么错误?
  • ACMBundle 中不存在键为“1, 2”的数组的键 ID:message:index.html.twig
  • 您是如何从变量c 中获取价值的?查看更新
  • 当我输入 1 而不是 id 时,它可以正常工作,但是当我输入 id 或 c.selectedIndex 时,我收到一个错误,例如 Key "+ id +" for array with keys "1, 2" " 不存在或变量 "id" 在 ...中不存在
  • 那是因为 getElementById 只会带回一个值。页面上应该只有 1 个同名 ID。如果您想为某些元素分配多个名称,您需要使用class="className"

标签: php jquery symfony


【解决方案1】:

如果您选择的元素是一个下拉列表,我建议使用 jQuery 的以下代码,因为您已经在使用它:

// Select by the ID predefinedMessage
var dropDown = $("select#predefinedMessage");

// Get the selected index from the dropdown
var selectedIndex = dropDown.find("option:selected").index();

var text = "{{  form.predefinedMessage.vars.choices[" + selectedIndex + "].data.message }}";

$('message').val(text);

【讨论】:

  • ACMBundle 中不存在键为“1, 2”的数组的键 ID:message:index.html.twig
  • 预定义的消息是一个下拉列表
  • Key " + selectedIndex + " 用于键为 "1, 2" 的数组不存在,正如我告诉你的,问题是我如何传递变量 id。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2016-01-17
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
相关资源
最近更新 更多