jQuery switch-case 不起作用,语法问题?

发布于 2024-12-26 01:47:50 字数 464 浏览 1 评论 0原文

为什么这个开关盒不工作?我已经尝试了所有可能的语法,但它不起作用。或者在这种情况下 switch-case 是错误的方法吗?

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name') {
        case 'email':
            jQuery(this).val('That's you e-Mail adress.');
            break;
        case 'nickname':
            jQuery(this).val('That's your nickname.');
            break;
    }
});

请注意:我在非冲突模式下使用 jQuery,这就是我使用 jQuery 而不是 $ 的原因。

Why is this switch-case not working? I've tried every possible syntax, but it is just not working. Or is a switch-case the wrong method in this case?

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name') {
        case 'email':
            jQuery(this).val('That's you e-Mail adress.');
            break;
        case 'nickname':
            jQuery(this).val('That's your nickname.');
            break;
    }
});

Please note: I am using jQuery in non conflict mode, that's why I use jQuery instead of $.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜嗑 2025-01-02 01:47:50

这里有几个错误:

switch(jQuery(this).attr('name') {

缺少右括号。应该是:

switch(jQuery(this).attr('name')) {

您应该转义该字符串中的引号:

jQuery(this).val('That's you e-Mail adress.');

像这样:

jQuery(this).val('That\'s you e-Mail adress.');

You have several errors in here:

switch(jQuery(this).attr('name') {

Missing a closing parenthesis. Should be:

switch(jQuery(this).attr('name')) {

You should escape the quote in this string:

jQuery(this).val('That's you e-Mail adress.');

Like this:

jQuery(this).val('That\'s you e-Mail adress.');
心安伴我暖 2025-01-02 01:47:50

在同一行中使用许多 ' 似乎会扰乱您的语法。更改如下,

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name')) {  // <-- fix 1: added closing )
        case 'email':
            jQuery(this).val("That's you e-Mail adress."); // <-- fix 2: quotes 
        break;
        case 'nickname':
            jQuery(this).val("That's your nickname.");  // <-- fix 3: quotes 
            break;
    }
});

Using many ' in the same line seems to mess your syntax. change as below,

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name')) {  // <-- fix 1: added closing )
        case 'email':
            jQuery(this).val("That's you e-Mail adress."); // <-- fix 2: quotes 
        break;
        case 'nickname':
            jQuery(this).val("That's your nickname.");  // <-- fix 3: quotes 
            break;
    }
});
过去的过去 2025-01-02 01:47:50

您有两种类型的错误:

  1. 缺少 switch 参数周围的括号 )
  2. 您没有转义单词“that's”中的撇号(')。

更正的代码:

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name')) { // Added a )
        case 'email':
            jQuery(this).val('That\'s you e-Mail adress.'); // Escaped the '
            break;
        case 'nickname':
            jQuery(this).val('That\'s your nickname.'); // Escaped the '
            break;
    }
});

更新:

如果您不熟悉 JavaScript 中的转义字符串(例如上面的单词 That's ),看看这个 Stack Overflow 问题:Escaping Strings in JavaScript

You have two types of errors:

  1. You are missing a parenthesis ) closing around the switch parameter.
  2. You are not escaping your apostrophes (') in the word "that's".

Corrected Code:

jQuery('#main input').blur(function() {
    switch(jQuery(this).attr('name')) { // Added a )
        case 'email':
            jQuery(this).val('That\'s you e-Mail adress.'); // Escaped the '
            break;
        case 'nickname':
            jQuery(this).val('That\'s your nickname.'); // Escaped the '
            break;
    }
});

Update:

If you are unfamiliar with escaping strings in JavaScript (such as with the word That's above), take a look at this Stack Overflow question: Escaping Strings in JavaScript

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