<script src="content/personSchedule.js"></script>
var personSchedule = [
{
"date": "02/6/2017",
"title": "CEO",
"mondayAM": "Apple Campus AM",
"mondayPM": "Apple Campus PM",
"tuesdayAM": "Apple Campus AM",
"tuesdayPM": "Apple Campus PM",
"wednesdayAM": "Apple Campus AM",
"wednesdayPM": "Apple Campus PM",
"thursdayAM": "Apple Campus AM",
"thursdayPM": "Apple Campus PM",
"fridayAM": "Apple Campus AM",
"fridayPM": "Apple Campus PM",
"saturdayAM": "Apple Campus AM",
"saturdayPM": "Apple Campus PM"
},
{
"date": "02/13/2017",
"title": "CEO",
"mondayAM": "Apple Campus AM",
"mondayPM": "Apple Campus PM",
"tuesdayAM": "Apple Campus AM",
"tuesdayPM": "Apple Campus PM",
"wednesdayAM": "Apple Campus AM",
"wednesdayPM": "Apple Campus PM",
"thursdayAM": "Apple Campus AM",
"thursdayPM": "Apple Campus PM",
"fridayAM": "Apple Campus AM",
"fridayPM": "Apple Campus PM",
"saturdayAM": "Apple Campus AM",
"saturdayPM": "Apple Campus PM"
}
];
我有一个日期选择器
<input type="date" id="date" step="7" min="2017-02-06" />
日期更改时,我想更改HTML中显示的文字
$("#date").change(function(){
var theSelectedDate = $('#date').val();
console.log(theSelectedDate);
$.each(json.people.person, function(i, v) {
if (v.name.search(new RegExp(/theSelectedDate/)) != -1) {
alert(v.title);
return;
}
});
});
当我运行时,我收到以下错误
未捕获的SyntaxError:无效的正则表达式:缺少/
答案 0 :(得分:1)
你写了/theSelectedDate
,我认为它应该是/theSelectedDate/
或其他一些RegExp格式。
答案 1 :(得分:1)
你的正则表达式模式错误:应该像新的RegExp(/ab+c/, 'i');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
在您的情况下,您需要RegExp(thSelectedDate)
,因为您将值传递给RegExp()
,并且该函数会通过在其前后附加\
来自动评估该值。
答案 2 :(得分:1)
您可能希望执行以下操作:
function getOjectForDate(date){
return myArray.filter(function(value){
return value.date == date
})
}
console.log(getOjectForDate(input.val()) // returns array with every valid object
如果您只想获得一个值或未定义,请使用Array.find
method。
其中myArray是JSON对象,输入是输入