带有 onChange 事件的 javascript 自动完成
我有两个输入字段,Id 和 Desc。我需要一个 JavaScript,它根据已经设置的一些字段在第二个文本字段中输入值。就像圣诞节一样,如果输入 12/25,则应该显示圣诞节。
它为一个值而工作。但如果我输入“else if”,它不适用于多个值。
<script type="text/javascript">
$(document).ready(function() {
$('#holidayDate').datepicker();
var availableTags = ["New years Day", "Martin Luther King Day", "Groundhog Day", "Valentine's Day", "Washington's Birthday", "Easter", "Earth Day", "National Arbor Day", "Mother's Day", "Memorial Day", "Flag Day", "Father's Day", "Independence Day", "Labor Day", "Columbus Day", "Halloween", "Veterans Day", "Thanksgiving Day", "Pearl Harbor Remembrance Day", "Christmas Day"];
$("#tags").autocomplete({source:availableTags});
$('#holidayDate').change(function() {
if ($(this).val().substring(0, 5) === '12/25') {
$('#tags').val('Christmas Day');
}else if ($(this).val().substring(0, 5) === '01/01') {
$('#tags').val('New years Day');
} else {
$('#tags').val('');
}
});
});
</script>
I have two input fields, Id and Desc. I need a javascript which enters the values in second text field based on some of the fields already set. Like for christmas day, if 12/25 is entered, it should say Christmas day.
Its working for one value. But if I put "else if" its not working for multiple values.
<script type="text/javascript">
$(document).ready(function() {
$('#holidayDate').datepicker();
var availableTags = ["New years Day", "Martin Luther King Day", "Groundhog Day", "Valentine's Day", "Washington's Birthday", "Easter", "Earth Day", "National Arbor Day", "Mother's Day", "Memorial Day", "Flag Day", "Father's Day", "Independence Day", "Labor Day", "Columbus Day", "Halloween", "Veterans Day", "Thanksgiving Day", "Pearl Harbor Remembrance Day", "Christmas Day"];
$("#tags").autocomplete({source:availableTags});
$('#holidayDate').change(function() {
if ($(this).val().substring(0, 5) === '12/25') {
$('#tags').val('Christmas Day');
}else if ($(this).val().substring(0, 5) === '01/01') {
$('#tags').val('New years Day');
} else {
$('#tags').val('');
}
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您可以将它们组合成一个 DOM 就绪事件。另外,您的某些代码位于事件之外,因此这可能就是它无法正常工作的原因。
更新:希望这只是一个写得不好的示例,但您给定的 HTML 也非常无效。请注意,您正在打开
标记,然后关闭
标记,以及其他问题,例如未关闭
标记等。所有这些问题都可能会阻止 javascript 正常运行。
First, you can combine those into one DOM ready event. Also, some of your code was outside the event, so that's probably why it wasn't working.
UPDATE: Hopefully it's just a poorly written example, but your given HTML is very invalid as well. Notice you are opening a
<div>
tag and then closing a<p>
tag, among other issues like not closing your<head>
tag before opening the body, etc. All of these issues could be preventing the javascript from functioning properly.我相信你在行中遗漏了一个撇号,它
应该读作
I believe you're missing an apostrophe in the line that says
it should read
也许你应该这样做:
我认为你的 onchange 事件没有被注册。
问候,
萨蒂亚吉特
Maybe you should do this:
I think your onchange event is not being registered.
Regards,
Satyajit