JQuery 使用复杂的选择器
JQuery use with complicated selectors
预先感谢您的帮助。
我试图操纵一个calendar existing airline tickets web page。
通过向函数输入开始日期和结束日期的方式,她将在日历上搜索当前页面并单击正确的日期元素。
我的主要问题是我在尝试合并一个月和一天时使用的 jquery 选择器,我在通话前不知道,因此 jquery 无法识别我制作的混合变量:
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
$("[aria-label=sDateVar]").click();
这就是全部代码:
/**
* A function to select start date and end date from Turkish Airlines
* website
* Assumption: The date-picker is already in an open position when running
* the code
* @param {String} startDate - with format dd/mm/yyyy
* @param {String} endDate - with format dd/mm/yyyy
* @returns {Boolean} success or not
*/
Function selectDateInTurkishAirlines(startDate, endDate){
//months names for later convert
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
//split the dates in different vars for easy use.
var startDate = startDate.Split('/');
var endDate = endDate.Split('/');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var nextClicksForStart = 12*(startDate[2]-yyyy) + (startDate[1]-mm);
var nextClicksForEnd = 12*(endDate[2]-startDate[2]) + (endDate[1]-startDate[1]);
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
var eDateVar = MONTH_NAMES[endDate[1]]+ ' ' +endDate[0];
//move to the current startDate month on the calendar and click the startDate
for (var nextCountS = 0; nextCountS < nextClicksForStart; nextCountS++) {
$("[data-handler='next']").click();
}
$("[aria-label=sDateVar]").click();
//move to the current endDate month on the calendar and click the endDate
for (var nextCountE = 0; nextCountE < nextClicksForEnd; nextCountE++) {
$("[data-handler='next']").click();
}
$("[aria-label=eDateVar").click();
}
再次感谢您的帮助或新方向。
您正在为选择器创建变量,但没有在选择器中使用它们:.
示例:
var myVar = 'something';
// you are doing like
$("[data-handler=myVar]");
// when you should be doing like
$("[data-handler=" + myVar + "]");
// which will be equivalent to $("[data-handler=something]");
当然要注意这些变量的值。如果他们有 "
或 ]
,则可能会使选择器无效。如果可能的话,请引用选择器:
$("[data-handler='" + myVar + "']");
同样,在此示例中,您假设变量没有 '
.
您的部分代码已更新:
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
var eDateVar = MONTH_NAMES[endDate[1]]+ ' ' +endDate[0];
//move to the current startDate month on the calendar and click the startDate
for (var nextCountS = 0; nextCountS < nextClicksForStart; nextCountS++) {
$("[data-handler='next']").click();
}
$("[aria-label='" + sDateVar + "']").click(); // changed
//move to the current endDate month on the calendar and click the endDate
for (var nextCountE = 0; nextCountE < nextClicksForEnd; nextCountE++) {
$("[data-handler='next']").click();
}
$("[aria-label='"+ eDateVar + "']").click(); // changed
我不太明白你的问题,但我看到了你可以解决的问题。
$('[arial-label="' + sDateVar + '"]')
也在这里:
$('[aria-label="' + eDateVar + '"]')
另一件事:不应该将 .click()
调用更改为 .trigger('click')
,因为我认为您想在那里模拟点击,对吗?
预先感谢您的帮助。 我试图操纵一个calendar existing airline tickets web page。 通过向函数输入开始日期和结束日期的方式,她将在日历上搜索当前页面并单击正确的日期元素。 我的主要问题是我在尝试合并一个月和一天时使用的 jquery 选择器,我在通话前不知道,因此 jquery 无法识别我制作的混合变量:
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
$("[aria-label=sDateVar]").click();
这就是全部代码:
/**
* A function to select start date and end date from Turkish Airlines
* website
* Assumption: The date-picker is already in an open position when running
* the code
* @param {String} startDate - with format dd/mm/yyyy
* @param {String} endDate - with format dd/mm/yyyy
* @returns {Boolean} success or not
*/
Function selectDateInTurkishAirlines(startDate, endDate){
//months names for later convert
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
//split the dates in different vars for easy use.
var startDate = startDate.Split('/');
var endDate = endDate.Split('/');
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var nextClicksForStart = 12*(startDate[2]-yyyy) + (startDate[1]-mm);
var nextClicksForEnd = 12*(endDate[2]-startDate[2]) + (endDate[1]-startDate[1]);
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
var eDateVar = MONTH_NAMES[endDate[1]]+ ' ' +endDate[0];
//move to the current startDate month on the calendar and click the startDate
for (var nextCountS = 0; nextCountS < nextClicksForStart; nextCountS++) {
$("[data-handler='next']").click();
}
$("[aria-label=sDateVar]").click();
//move to the current endDate month on the calendar and click the endDate
for (var nextCountE = 0; nextCountE < nextClicksForEnd; nextCountE++) {
$("[data-handler='next']").click();
}
$("[aria-label=eDateVar").click();
}
再次感谢您的帮助或新方向。
您正在为选择器创建变量,但没有在选择器中使用它们:.
示例:
var myVar = 'something';
// you are doing like
$("[data-handler=myVar]");
// when you should be doing like
$("[data-handler=" + myVar + "]");
// which will be equivalent to $("[data-handler=something]");
当然要注意这些变量的值。如果他们有 "
或 ]
,则可能会使选择器无效。如果可能的话,请引用选择器:
$("[data-handler='" + myVar + "']");
同样,在此示例中,您假设变量没有 '
.
您的部分代码已更新:
var sDateVar = MONTH_NAMES[startDate[1]]+ ' ' +startDate[0];
var eDateVar = MONTH_NAMES[endDate[1]]+ ' ' +endDate[0];
//move to the current startDate month on the calendar and click the startDate
for (var nextCountS = 0; nextCountS < nextClicksForStart; nextCountS++) {
$("[data-handler='next']").click();
}
$("[aria-label='" + sDateVar + "']").click(); // changed
//move to the current endDate month on the calendar and click the endDate
for (var nextCountE = 0; nextCountE < nextClicksForEnd; nextCountE++) {
$("[data-handler='next']").click();
}
$("[aria-label='"+ eDateVar + "']").click(); // changed
我不太明白你的问题,但我看到了你可以解决的问题。
$('[arial-label="' + sDateVar + '"]')
也在这里:
$('[aria-label="' + eDateVar + '"]')
另一件事:不应该将 .click()
调用更改为 .trigger('click')
,因为我认为您想在那里模拟点击,对吗?