jQuery多个ID相同的功能 [英] jquery multiple ids same function
本文介绍了jQuery多个ID相同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有日期输入的表
I have a table that has a date input
<td style="background-color:#c6efce"><input type="text" id="datepicker0"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker1"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker2"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker3"></td>
<td style="background-color:#c6efce"><input type="text" id="datepicker4"></td>
我正在尝试通过第一个访问它
I am trying to access it via for the first one
<script>
$(function() {
$( "#datepicker0" ).datepicker({
showButtonPanel: true
});
});
</script>
我如何访问所有内容?
推荐答案
您可以使用"属性开始-使用"选择器:
You could use the "attribute starts-with" selector:
$(function() {
$("input[id^='datepicker']").datepicker({
showButtonPanel: true
});
});
该选择器将匹配其id
值以"datepicker"开头的任何input
元素.一种替代方法是为所有必需的元素提供一个通用类.
That selector will match any input
element whose id
value starts with "datepicker". An alternative would be to give all the required elements a common class.
您还可以使用逗号分隔列表通过id
选择多个元素:
You can also select multiple elements by id
using a comma-separated list:
$("#datepicker0, #datepicker1, #datepicker2"); //List as many as necessary
但是,如果您需要添加更多输入,那并不是特别可扩展.
But that's not particularly scalable if you ever need to add more inputs.
这篇关于jQuery多个ID相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文