通过ID从变量中选择元素 [英] Select element by ID from variable
本文介绍了通过ID从变量中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当名称在变量上时,如何选择输入?在这种情况下,变量为idThis = nico
How can I select an input when the name is on a variable? in this case the variable is idThis = "nico"
<input name="man-news" />
<input id="nico" name="milkman" />
<input name="letterman2" />
<input name="newmilk" />
<script>
var idThis = "nico";
$('input[id=idThis ]').val('has man in it!');
</script>
谢谢!
推荐答案
您可以构造一个选择器字符串:
You can either construct a selector string:
$('#' + idThis)
或包装本机DOM方法:
Or wrap a native DOM method:
$(document.getElementById(idThis))
第二个适用于任何id,而如果id中包含空格或点,则第一个将中断。
The second one works for any id, while the first one will break if the id has spaces or dots in it.
这篇关于通过ID从变量中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文