开发者

jquery-ajax help to improve function

I have created following function to fill field of a form page, there is a way to use it several time in one page with multiple form?

function scorriDati(xml)
{
    var forma=$("form");
    var elements=$("*", forma);
$.each(elements,function(i){
    var id = $(elements[i]).attr("id");
    var tipo = $(elements[i]).attr("type");
    var nome=$(elements[i]).attr("name");
    var val=$(elements[i]).attr("value");
    switch (tipo)
    {
        case "text" :
            $(elements[i]).val($(id, xml).text());
        break;
        case "hidden" :
            $(elements[i]).val($(id, xml).text());
        break;
        case "textarea" :
            $(elements[i]).append($(id, xml).text());
        break;
        case "radio":
            $("input:radio[name='"+nome+"'][value='"+ $(nome, xml).text() +"']").attr('checked', true);
        break;
        case "checkbox":
            $(xml).find(nome).each(function(){ 
            var value =$(this).text();
            if (val==value)
            {
                    $("#"+id).attr("checked", value); // checked="checked" or checked="" 
                }
            }); 

        break;
        case "select-one" :
            var lung = $(xml).find(nome).length;
            if(lung==1)
            {
                $(xml).find(nome).each(function()
                {
                    if (($(this).find("optionValue").text() != "") && ($(this).find("optionDisplay").text() !=""))
                    {
                        $(elements[i]).append("<option value='"+$(this).find("optionValue").text()+"' selected>"+$(this).find("optionDisplay").text()+"</option><br>\n");
                    }
                else
                    {
                        var value =$(this).text();
                        $(elements[i]).append("<option value='"+value+"' selected>"+value+"</option>");
                    }
                });
            }
            else
            {
                $(xml).find(nome).each(function()
                {
                    $(elements[i]).append("<option value='"+$(this).find("optionValue").text()+"'>"+$(this).find("optionDisplay").text()+"</option><br>\n");
                });
            }
        break;
        case "select-multiple" :
            $(xml).find(nome).each(function(){
            var value =$(this).text();
                $("#"+id).append("<option>"+value+"</option>");
            });
        break;

    }
});
 }

I use this function in this way:

$("#div_cont").load("./schemi/sch_anag.php");
        $.post("./php/cliente.php",
         开发者_如何学C   {azione:"vedi", contratto:contratto},
            function(xml)
            {
                scorriDati(xml);
                $("#div_cont").append("<div style='clear:both'><div class='bt' id='del_"+contratto+"'>elimina</div> <div class='bt' id='mod_"+contratto+"'>VALIDA</div></div>");
            },
            "xml"
        );


Just define the function with:

function scorriDati(xml, formId) {
  var forma = $('#' + formId);
  //...

... and give each form an id.


If your XML contains all the IDs of the form elements your code should work as is.

One other thing: For performance reasons you should store $(elements[i]) and $(id, xml) in a variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜