简体   繁体   English

单击链接后增加ID的值

[英]increase value of an ID after clicking a link

I am using a date picker "start date" and with "end date", also in my php page I am using "add more field", I want to, when i am clicking on add more it should also increase date id, below is code which will give u better idea. 我正在使用日期选择器“开始日期”和“结束日期”,也在我的php页面中,我正在使用“添加更多字段”,我想,当我单击“添加更多”时,它也应该增加日期ID,如下是可以给您更好主意的代码。

<script>
if (top.location != location) {
top.location.href = document.location.href ;
}
$(function(){
window.prettyPrint && prettyPrint();

// disabling dates var nowTemp = new Date(); //禁用日期var nowTemp = new Date(); var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0); var now = new Date(nowTemp.getFullYear(),nowTemp.getMonth(),nowTemp.getDate(),0、0、0、0);

var value = 1;   
value++;

var checkin = $('#dpd1').html(value).datepicker({
onRender: function(date) {
return date.valueOf() < now.valueOf() ? 'disabled' : '';
}
}).on('changeDate', function(ev) {
if (ev.date.valueOf() > checkout.date.valueOf()) {
var newDate = new Date(ev.date)
newDate.setDate(newDate.getDate() + 1);
checkout.setValue(newDate);
}
checkin.hide();
$('#dpd2').html(value)[0].focus();
}).data('datepicker');
var checkout = $('#dpd2').html(value).datepicker({
onRender: function(date) {
return date.valueOf() <= checkin.date.valueOf() ? 'disabled' :     '';
}
}).on('changeDate', function(ev) {
checkout.hide();
}).data('datepicker');
});
</script>

This is my code in footer of the page, Now what i want when click on "add more row" .. code is below.. 这是我在页面页脚中的代码,现在单击“添加更多行”时我想要的..代码如下。

<table><tr><td><a href="#" title="" class="add-touring">Add more row...</a></td></tr></table>

Its should add 1, 2, 3.... as many times i click on "add more row". 它应该添加1、2、3 ...,因为我多次单击“添加更多行”。 I mean it should work like "#dpd1" should #dpd12, #dpd13, #dpd14..and so on. 我的意思是它应该像“#dpd1”应该#dpd12,#dpd13,#dpd14 ..等工作。

Actually when im clicking on "Add More row" ... its added a new row, just below 1st row, and again if im clicking again adding another row, so its like if my 1st "start date" id is id="dpd1" and end date id is id ="dpd2", it become in second row start date id="dpd12" and end date id ="dpd22" and in 3rd row its become start date id="dpd13" and end date id ="dpd23" .... its happening when im clicking add more row, in javascript its still only working with start date id="dpd1" and end date id="dpd2", and thats why its not working with 2nd row and 3rd row and so on, as u can see it in my jscript, what i want as my row working and with start date and end date increasing/adding value in id, the same way when i click on "add more row" its also add same way in my javascript, then my datepicker will work with every row, when i will click on add more row. 实际上,当我点击“添加更多行” ...时,它添加了一个新行,紧挨着第一行,如果我再次单击则添加了另一行,所以就像我的第一个“开始日期” id是id =“ dpd1 “,结束日期id为id =” dpd2“,它在第二行开始日期id =” dpd12“和结束日期id =” dpd22“,在第三行中成为开始日期id =” dpd13“和结束日期id = “ dpd23” ....它在我单击添加更多行时发生,在javascript中它仍然仅适用于开始日期id =“ dpd1”和结束日期id =“ dpd2”,这就是为什么它不适用于第二行和第三行行,依此类推,就像您在我的jscript中看到的那样,我想作为行工作的对象,并在id中增加开始日期和结束日期/在id中添加值,就像单击“添加更多行”时一样用我的javascript中的相同方法,那么当我单击添加更多行时,datepicker将适用于每一行。 for now its working for only first row. 现在,它仅在第一行工作。

I hope all of u can understand and give me a proper solution. 我希望你们所有人都能理解并给我适当的解决方案。

Thank you so much! 非常感谢!

If I understand your question correctly, you aren't actually trying to change the ID of a specific element, but you are trying to increment the SELECTOR so that it selects the appropriate element. 如果我正确理解了您的问题,那么您实际上并不是在尝试更改特定元素的ID,而是在尝试增加SELECTOR,以便它选择适当的元素。 Is that correct? 那是对的吗?

If that is the case, you can use a variable in your selector. 如果是这样,您可以在选择器中使用一个变量。 Instead of doing: 而不是做:

$("#dpd2").html(value).focus(); 

You could do something like: 您可以执行以下操作:

var currentDatePickerID = 0; 

And increment it each time your button is clicked, and then your selector would look like: 并在每次单击按钮时增加它,然后您的选择器将如下所示:

$("#dpd" + currentDatePickerID).html(value).focus(); 

That way your selector is dynamic. 这样,您的选择器便是动态的。

Here is solution.. which i implemented in my script.. 这是解决方案..我在我的脚本中实现的..

var counterTouring = 1;
$('.add-touring').click(function(event){
event.preventDefault();
counterTouring++;

var checkin = $('#dpd1' + counterTouring).html(value).datepicker({ 
//---- 
});

Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM