0

我有一个动态生成的表单,并且动态生成了 id(和潜在的类)。表格是相同的,但它们的最后都附加了相关的 ID。

如何选择每组并将代码应用于每组?

switch(id_type_champ)
            {
            case 1:
                 $('#header-right-container').append(''+nom_champ+'<input type="text" id='+Id+'  >  ').trigger("create");
              break;
            case 2:
                 $('#header-right-container').append(''+nom_champ+'<input type="text" id='+Id+' > ').trigger("create");
              break;
            case 3:
                 $('#header-right-container').append(''+nom_champ+'<input type="date" id='+Id+' OnClick="aff_date(\'' + Id + '\')">').trigger("create");
              break;
            case 4:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+'    ><option value=0></option></select>').trigger("create");

                v++;
                 break;
            case 5:
                $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 6:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 7:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 8:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 9:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            default:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
             break;
            }

该代码可以执行添加选项

4

2 回答 2

0

您可以像这样附加案例:

switch (id_type_champ) {
    case 1:
    case 2:
        $('#header-right-container').append(nom_champ + '<input type="text" id=' + Id + ' > ').trigger("create");
        break;
    case 3:
        $('#header-right-container').append(nom_champ + '<input type="date" id=' + Id + ' OnClick="aff_date(\'' + Id + '\')">').trigger("create");
        break;
    // The cases below can be removed, except dor "default"
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    // ^ These can be removed.
    default:
        $('#header-right-container').append(nom_champ + '<select id=' + Id + ' ><option value=0> </option></select> ').trigger("create");

        v++;
        break;
}

案例 1 到 3 将创建 a input, 4 到 9 和默认 a select
不过,您可以删除案例 4-9,因为默认情况下会处理这些案例。

此外,不需要在nom_champ( )前添加空字符串。'' + nom_champ我假设那是用来转换nom_champ为字符串的,但是'<input/select'附加的那会处理这个问题。

于 2012-11-21T08:48:21.677 回答
0

最好为此创建自己的插件,例如此处的原始函数:

function ListIds(var Id) {
switch(id_type_champ)
            {
            case 1:
                 $('#header-right-container').append(''+nom_champ+'<input type="text" id='+Id+'  >  ').trigger("create");
              break;
            case 2:
                 $('#header-right-container').append(''+nom_champ+'<input type="text" id='+Id+' > ').trigger("create");
              break;
            case 3:
                 $('#header-right-container').append(''+nom_champ+'<input type="date" id='+Id+' OnClick="aff_date(\'' + Id + '\')">').trigger("create");
              break;
            case 4:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+'    ><option value=0></option></select>').trigger("create");

                v++;
                 break;
            case 5:
                $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 6:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 7:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 8:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            case 9:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
              break;
            default:
                 $('#header-right-container').append(''+nom_champ+'<select id='+Id+' ><option value=0> </option></select> ').trigger("create");

                v++;
             break;
            }

}

然后,插件调用此代码:

    (function($){  
     $.fn.listIdValue = function() {  

        return this.each(function() {  
             ListIds($(this).id)

        });  
     };  
    })(jQuery); 

然后您可以在您的元素或元素列表上调用此函数,例如:

$('#element').listIdValue();

或者

$('.elementClass').listIdValue();
于 2012-11-21T08:55:34.393 回答