使用 jquery 填充输入字段,使用 twig 值
Fill input field, with twig value using jquery
我已经搜索了一段时间,但没有找到。
我知道我的问题看起来很普通,google 和这里有很多(数百个)答案,但我的问题似乎还远未解决。
我遇到以下问题,我需要使用来自 twig 文件中的 silex 变量的值来填充输入字段。
表格如下:
<span>Author: </span>
<input type="text" name="author" id="author"
{% if user.getAuthorName %}value="{{user.getAuthorName}}"{% endif %} />
<span><a href="#" id="filler">Use your own username</a></span>
js代码为:
<script>
$(function(){
$('#filler').live('click', function() {
$("#author").val($("#author").text("{{user.getFirstName}} {{user.getLastName}}"));
});
});
</script>
我遇到的问题是,代码确实有效,但当我单击时,它仍然用
填充字段
[object Object]
而不是2个变量的实际值
我试过将其更改为带有 id 的隐藏字段,并将变量设置为值,然后使用
.text($("#idfield").val())
仍然没有运气,仍然用
填充值
[object Object]
你们知道我做错了什么吗?
您应该删除双重声明:
$("#author").val("{{user.getFirstName}} {{user.getLastName}}");
这里是您的代码的稍微清理过的版本,没有 live
,因为它是 deprecated/removed。
$(function() {
$('#filler').on('click', function(e) {
e.preventDefault();
//If your JS is parsed as a twig template then, you could use
$("#author").val("{{user.getFirstName}} {{user.getLastName}}");
//and remove the data-full-name from your HTML
//If not, keep the data-full-name and use:
$("#author").val($("#author").data("fullName"));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>Author: </span>
<input data-full-name="{{user.getFirstName}} {{user.getLastName}}" type="text" name="author" id="author" value="{% if user.getAuthorName %}{{user.getAuthorName}}{% endif %}" />
<span><a href="#" id="filler">Use your own username</a></span>
我已经搜索了一段时间,但没有找到。
我知道我的问题看起来很普通,google 和这里有很多(数百个)答案,但我的问题似乎还远未解决。
我遇到以下问题,我需要使用来自 twig 文件中的 silex 变量的值来填充输入字段。
表格如下:
<span>Author: </span>
<input type="text" name="author" id="author"
{% if user.getAuthorName %}value="{{user.getAuthorName}}"{% endif %} />
<span><a href="#" id="filler">Use your own username</a></span>
js代码为:
<script>
$(function(){
$('#filler').live('click', function() {
$("#author").val($("#author").text("{{user.getFirstName}} {{user.getLastName}}"));
});
});
</script>
我遇到的问题是,代码确实有效,但当我单击时,它仍然用
填充字段[object Object]
而不是2个变量的实际值
我试过将其更改为带有 id 的隐藏字段,并将变量设置为值,然后使用 .text($("#idfield").val())
仍然没有运气,仍然用
填充值[object Object]
你们知道我做错了什么吗?
您应该删除双重声明:
$("#author").val("{{user.getFirstName}} {{user.getLastName}}");
这里是您的代码的稍微清理过的版本,没有 live
,因为它是 deprecated/removed。
$(function() {
$('#filler').on('click', function(e) {
e.preventDefault();
//If your JS is parsed as a twig template then, you could use
$("#author").val("{{user.getFirstName}} {{user.getLastName}}");
//and remove the data-full-name from your HTML
//If not, keep the data-full-name and use:
$("#author").val($("#author").data("fullName"));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>Author: </span>
<input data-full-name="{{user.getFirstName}} {{user.getLastName}}" type="text" name="author" id="author" value="{% if user.getAuthorName %}{{user.getAuthorName}}{% endif %}" />
<span><a href="#" id="filler">Use your own username</a></span>