串联在jQuery选择器中 [英] Concatenate in jQuery Selector
问题描述
简单的问题.我有一个js变量,我也想在jQuery选择器中串联.但是,它不起作用.也没有错误弹出.怎么了?如何在jQuery选择器中将变量正确连接到某些文本.
Simple problem. I have a js variable I want to be concatenated too within a jQuery selector. However it is not working. No errors are popping up either. What is wrong? How do I correctly concatenate a variable to some text in a jQuery selector.
<div id="part2"></div>
<script type="text/javascript" >
$('#button').click(function ()
{
var text = $('#text').val();
var number = 2;
$.post('ajaxskeleton.php', {
red: text
},
function(){
$('#part' + number).html(text);
});
});
</script>
推荐答案
$('#part' + number).html(text);
jQuery接受字符串(通常是CSS选择器)或 DOM节点作为参数来创建 jQuery对象.
jQuery accepts a String (usually a CSS Selector) or a DOM Node as parameter to create a jQuery Object.
在您的情况下,您应该将字符串传递给$()
In your case you should pass a String to $()
that is
$(<a string>)
确保可以访问变量number
和text
.
Make sure you have access to the variables number
and text
.
要测试,请执行以下操作:
To test do:
function(){
alert(number + ":" + text);//or use console.log(number + ":" + text)
$('#part' + number).html(text);
});
如果看到您没有访问权限,请将其作为参数传递给函数,则必须包括$ .get的常规参数,并在其后传递自定义参数.
If you see you dont have access, pass them as parameters to the function, you have to include the uual parameters for $.get and pass the custom parameters after them.
这篇关于串联在jQuery选择器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!