open a new window with post values enhancements
var projectWindow;
function btnNew_Click() {
var form = document.createElement("form");
form.setAttribute("target", "projectWindow");
form.setAttribute("method", "post");
form.setAttribute("action", "MySite.aspx");
document.body.appendChild(form);
var hiddenField = document.createElement("input");
hiddenField.setAttribute("id", "hiddenValue");
hiddenField.setAttribute("name", "ID");
hiddenField开发者_JS百科.setAttribute("value", "/*get value from javascript*/");
form.appendChild(hiddenField);
//Below line I added to fix the new input text field that was visible
document.getElementById("hiddenValue").style.visibility = 'hidden';
form.submit();
//The below line i added to give focus if another window is created
// The below code does not work
projectWindow.focus();
//I also tried this:
form.focus();
}
My problem in the above code that i obtained from the thread: Javascript Post on Form Submit open a new window
is that there is an input field that is shown at the client that I cant hide, except for the first time with the line I added (maybe this is about many values return from the method getElementbyId).
The second problem is that I want to set focus in the newly created or reloaded window, currently it is only working with the new window.
Try adding it to the onload
of the new window:
projectWindow.onload = function(){projectWindow.focus()};
Didn't test this code, hope this helps! Oh! and to create a hidden input, do this:
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
Also, getElementById
should not return many values, just one! If it does return more than one, there is something wrong.
To add on Victor's answer. The function btnNew_Click
always create a new form element.
You either have to check for it's existence and not recreate or destroy it after submitting it.
精彩评论