[英]Break attribute using jQuery
我正在使用PHP生成一些<input type="text" class="date" name="date'.$i.'">
字段,其中$ i通過我正在使用的foreach
函數遞增。 所以我會有這樣的事情:
<input type="text" class="date" name="date1">
<input type="text" class="date" name="date2">
<input type="text" class="date" name="date3">
我正在使用jQuery .last()
函數來選擇具有類date
的最后一個元素,但是我需要找到一種方法來在date
之后從name
屬性中獲取該數字。 我知道我需要使用.attr()
來獲取名稱,但我不知道如何分割它並得到數字。
此Regexp將僅返回number
var的數字。 它適用於任何name
。
var name = $(this).attr("name");
var number = name.replace(/\d+/g, "");
注意:當this
等於$(".date").last();
時, this
可以工作.last $(".date").last();
嘗試:
var name = "date1";
var number = name.replace(/date/g, '');
console.log(number);
它從字符串中刪除“日期”,為您留下您想要的數字。
$('.date').last().attr('name').substr(4);
但建議使用數據可能會更好。
好吧,如果$i
只是遞增,你可以簡單地使用集合的.length
而不是獲取.last()
屬性嗎?
$('input.date').length //the desired value
或.length - 1
,顯然,如果你從0開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.