开发者

Javascript : Submitting a form outside the actual form doesn't work

I'm trying to achieve a fairly easy triggering mechanism for deleting multiple items from a tablegrid. If a user has enough access he/she is able to delete multiple users from a table. In the table I have set up checkboxes, one per row/user. The name of the checkboxes is UsersToDeletep[], and the value per row is the unique UserID.

When a user clicks the button 'Delete selected users' a simple validation takes place to make sure at least one ch开发者_开发技巧eckbox is selected. After that I call my simple function Submit(form). The function works perfectly when called within the form-tags, where I also use it to delete a single user.

The function:

function Submit(form)
{   
    document.forms[form].submit();
}

I've also alerted document.forms[form]. The result is, as expected [object HTMLFormElement]. But for some reason the form just won't submit and a pagereload takes place. I'm a bit confused and can't seem to figure out what I'm doing wrong.

Update

As requested: the calling-code:

The code placed within onclick="javascript:":

if(CheckMinimumCheckedCheckboxes('FormUserList', 1) == true)
{ 
    if(confirm('Weet u zeker dat u de geselecteerde gebruiker(s) wilt verwijderen?')) 
    { 
        Submit('FormUserList'); 
    } 
} 
else 
{ 
    alert('U dient minimaal 1 gebruiker te selecteren welke u wilt verwijderen.'); 
}

Separate functions in an external JS-file.

function CheckMinimumCheckedCheckboxes(formName, minAmount)
{
    var totalChecked = 0;
    var totalElements = document.forms[formName].length;

    for(i = 0; i < totalElements; i++)
    {
        if(document.forms[formName][i].checked == true)     
        {
            totalChecked++;
        }
    }

    if(totalChecked >= minAmount)
    {
        return true;
    }

    return false;
}

function Submit(form)
{   
    document.getElementById(form).submit();
}


Try using the getElementById() Method:

http://www.w3schools.com/jsref/met_doc_getelementbyid.asp

document.getElementById("frm1").submit();

http://www.w3schools.com/jsref/met_form_submit.asp


Never, ever, ever place an empty 'href=""' in your links and think you will fill it in later while later you decide to handle the link-element by an onclick-eventhandler... Seriously, it can save you hours of silly testing. :/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜