我有以下jquery代码:
$(document).ready(function(){
var request_text;
request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
$('textarea').text(request_text);
$('#name').keyup(function() {
$('textarea').text(request_text);
});
});
它适用于document.ready部分,但不适用于keyup。我认为这是一个范围问题,但我似乎无法让它发挥作用。
如果我执行以下操作,则可以:
$('#name').keyup(function() {
request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
$('textarea').text(request_text);
});
但我不想重复代码。
有什么想法吗?
答案 0 :(得分:2)
当然你要在request_text
处理程序中包含keyup
字符串的生成 - 你怎么期望textarea的内容改变呢?< / p>
至于不重复代码:你并不需要keyup
处理程序之外的所有代码。你可以这样做:
$(document).ready(function(){
$('#name').keyup(function() {
var request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
$('textarea').text(request_text);
}).keyup();
});
注意所有逻辑现在都在keyup
处理程序中,以及如何在处理程序设置之后我们用.keyup()
在现场触发它,以便textarea得到它的“初始”用户实际按任意键之前的值。
除了上面的解决方案(可以说它是惯用的jQuery并且不适用于所有地方),还有一般的:当你不想重复代码时,写一个函数!
$(document).ready(function(){
var setRequestText = function() {
var request_text;
request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
$('textarea').text(request_text);
};
setRequestText();
$('#name').keyup(setRequestText);
});
答案 1 :(得分:1)
试试这个
$(document).ready(function(){
$('textarea').text(test());
$('#name').keyup(function() {
$('textarea').text(test());
});
});
function test(){
var request_text;
request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
return request_text;
}
答案 2 :(得分:0)
我认为该行:
var request_text;
行前的:
$(document).ready(function(){
将解决问题,即在全局范围内定义变量:)
答案 3 :(得分:0)
这不是范围问题。 $(document).ready中的代码在页面加载时运行一次。它不是一种功能。
答案 4 :(得分:0)
如果不工作就意味着$('#name')。val()部分没有改变,你可以将request_text生成行包装成lambda。
var request_text = function() { return "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name.capitalize %>.\n\n" +
"Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val(); }
$('textarea').text(request_text());
$('#name').keyup(function() {
$('textarea').text(request_text());
});
答案 5 :(得分:0)
有多种方法可以使变量成为全局变量:
var
关键字。这适用于任何地方。window.VARNAME
声明您的变量。默认情况下,所有全局变量都是window
对象的一部分。它应该使它成为一个全局变量因此,在您的示例中,您可以执行以下操作之一
var
window.request_text
作为变量名称答案 6 :(得分:0)
您需要在keyup函数中更新request_text变量的值。 Here是一个演示。