带有 onChange 事件的 javascript 自动完成

发布于 2024-09-09 07:29:06 字数 1073 浏览 7 评论 0原文

我有两个输入字段,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-09-16 07:29:06

首先,您可以将它们组合成一个 DOM 就绪事件。另外,您的某些代码位于事件之外,因此这可能就是它无法正常工作的原因。

$(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('');
        }
    });
});

更新:希望这只是一个写得不好的示例,但您给定的 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.

$(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. 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.

﹉夏雨初晴づ 2024-09-16 07:29:06

我相信你在行中遗漏了一个撇号,它

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

应该读作

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

I believe you're missing an apostrophe in the line that says

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

it should read

$('#desc').val('Christmas');
久而酒知 2024-09-16 07:29:06

也许你应该这样做:

<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>

我认为你的 onchange 事件没有被注册。

问候,
萨蒂亚吉特

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.

Regards,
Satyajit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文