简体   繁体   中英

Form with onSubmit on a JSP

I've got this form on a JSP:

<form name="<%=formName%>" method="post" action="<%=actionURL%>" target="_parent" onsubmit="submitForm('<%=String.valueOf(isAbstract)%>','<%=formName%>'); return false">

Then I have the following Javascript in an include file:

function submitForm( isAbstract , formName ) {
    var o = "document.forms['" + formName + "']";
    var form = eval(o);
    if (isAbstract)
    {
            alert("Error");
            return;
    }
    if (!form.AutoName.checked)
    {
        if (form.name.value == "")
        {
            alert("Some text");
            return;
        }
    }
}

Well, I reduced the functions to make it simpler, but the thing is that whatever I do, I keep getting undefined values for both arguments. This is driving me crazy, because I've got similar code on other tags that work just fine (except that it's on a combo box tag with the onChange action).

Any thoughts, please?

I see that you are submitting the form to parent frame by using target attribute. Are you submitting the values from child iframe to parent window? If yes, does this form exist in parent frame?

Alterantively, you can simply pass "this" and access that as a form object.

For example,

<form onsubmit="submitForm(this)">

function submitForm(formObj){
  var fieldTxt1Value = formObj.txt1.value;

}

try to place a return false; At end of your func.

Looking your code, you're testing if the first parameter is true. But it is a string all the time, because you write quotes in call. It's what you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM