关于(a)表单和(b)提示窗口的两个Javascript问题

I have two questions about Javascript:

  • How can I prevent my fields from being cleared when I try to cancel a form submission by returning false in the onsubmit function?

I have a form, which has the onsubmit value "return validate()". If validate() finds an error in the fields, it returns false, which should stop the form submission. It does, but all my fields get cleared, and the browser ignores the Javascript I run before the return statement, like document.write. Example:

function validate() {
//If there's an error:
document.write("Error!");
return false;       }

How can I prevent this?

  • Can I have a form inside a prompt window (a popup)?

I have not only failed to put several inputs in one Javascript prompt(), but also to put a submittable form inside a prompt window. I have a feeling that this is not possible with Javascript, and would like to know how to achieve this.

Thanks.

onsubmit="return validate()" is fine. There must be another reason your fields are being cleared. What does your validate() function do? Unless your submit button is actually a reset() button - or there's a JavaScript error somewhere and the form is being submitted regardless (check the JavaScript console in the browser).

1) Use alert or elements inner text to display error than document.write. Document write clear the DOM and write the specified content so you will loose the form data.

Use

function validate() {
//If there's an error:
document.getElementById('errorDiv').innerText = 'your error message';
return false;       }

2) If you talking about native prompt window, then answer is no. If you talking about JS pop--up then yes, u can put form inside pop up. Pop is just like any browser window where u can show any html page.