简体   繁体   English

带有onChange事件的javascript自动完成功能

[英]javascript autocomplete with onChange event

I have two input fields, Id and Desc. 我有两个输入字段,Id和Desc。 I need a javascript which enters the values in second text field based on some of the fields already set. 我需要一个javascript,它根据已设置的某些字段在第二个文本字段中输入值。 Like for christmas day, if 12/25 is entered, it should say Christmas day. 就像圣诞节一样,如果输入12/25,则应该说圣诞节。

Its working for one value. 它为一个价值工作。 But if I put "else if" its not working for multiple values. 但是,如果我输入“ 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>

First, you can combine those into one DOM ready event. 首先,您可以将它们合并为一个DOM ready事件。 Also, some of your code was outside the event, so that's probably why it wasn't working. 另外,您的某些代码不在事件范围内,所以这可能就是为什么它不起作用的原因。

$(function() {
    $('#date').datepicker();
    var availableTags = ["Christmas Day","etc"];
    $("#tags").autocomplete({source:availableTags});
    $('#date').change(function() {
        if ($(this).val().substring(0, 5) === '12/25') {
            $('#desc').val('Christmas');
        } else {
            $('#tags').val('');
        }
    });
});

UPDATE : Hopefully it's just a poorly written example, but your given HTML is very invalid as well. 更新 :希望这只是一个写得不好的例子,但是您给定的HTML也是非常无效的。 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. 请注意,您正在打开<div>标记,然后关闭<p>标记,以及诸如在打开主体之前不关闭<head>标记之类的其他问题。所有这些问题都可能阻止javascript正常运行。

I believe you're missing an apostrophe in the line that says 我相信您在一行中缺少撇号

$('#desc).val('Christmas');

it should read 它应该读

$('#desc').val('Christmas');

Maybe you should do this: 也许你应该这样做:

<DIV class="ui-widget">Date: <INPUT TYPE="TEXT" name="xx" property="xx" id="date" /></P>
<input type="text" id="desc" name="desc"/>
<script>
$('#date').change( function() { 
    if ($(this).val().substring(0, 5) === '12/25') { 
        $('#desc').val('Christmas'); 
    } 
});   
</script>
</body>

I think your onchange event is not being registered. 我认为您的onchange事件尚未注册。

Regards, Satyajit 问候,萨蒂亚吉特

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

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