开发者

jQuery fill form with data form array problem

I have the function below that I am trying to make it fill a form with a specified ID.

function input_form(form, data_array)
{
$.each(data_array, function(key, value){
    //alert(key + " = " + value);
    $(form).fi开发者_StackOverflow中文版nd('input[name="'+key+'"]').each(function()
    {
        switch(this.type)
        {
            case 'select-one':
            case 'select-multiple':
                $('input[name="'+key+'"] value:contains("'+value+'")').attr('selected','selected');
                break;
            case 'password':
            case 'text':
            case 'textarea':
                //alert(key + " = " + value);
                $('input[name="'+key+'"]').val(value);
                break;
            case 'checkbox':
            case 'radio':
                $('input[name="'+key+'"] value:name("'+value+'")').attr('checked','checked');
            //this.checked = true;
        }
    });
});
}

I have the following problems:

  1. It fills in text boxes fine, but dont work for radios and checkboxes.

  2. I have another form with the same name values and its also filling in that form even tho the form has a different ID.

var form = '#form1'

var data_array = the keys are the name values of the form to be filled in by the values.

Can anyone see where I am going wrong or if it has anything else wrong with it.

Thanks.

SOLUTION

Thanks to Matt BAll for cleaner code. I edited for radio and checkboxes as it was selecting the last radio instead of actual selected one.

function input_form(form, data_array)
{
    $.each(data_array, function(key, value){
        //alert(key + " = " + value);
        $(form).find('input[name="'+key+'"]').each(function()
        {
            var $this = $(this);
            switch(this.type)
            {
                case 'select-one':
                case 'select-multiple':
                    $this.attr('selected', true);
                    break;
                case 'password':
                case 'text':
                case 'textarea':
                    $this.val(value);
                    break;
                case 'checkbox':
                case 'radio':
                    //
                    if($(this).val() == value)
                    {
                        this.checked = true;
                    }
            }
        });
    });
}


Try this on for size.

function input_form(form, data_array)
{
    $.each(data_array, function(key, value){
        //alert(key + " = " + value);
        $(form).find('input[name="'+key+'"]').each(function()
        {
            var $this = $(this);
            switch(this.type)
            {
                case 'select-one':
                case 'select-multiple':
                    $this.attr('selected', true);
                    break;
                case 'password':
                case 'text':
                case 'textarea':
                    $this.val(value);
                    break;
                case 'checkbox':
                case 'radio':
                    this.checked = true;
            }
        });
    });
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜