Is anyone aware of what variables go into a form to make the iPhones virtual keyboard\'s GO button submit the form vs. not?
I\'ve been trying to narrow down the scen
Just enclosing my input type='search' in a form tag did the trick when I encountered this problem. Hopefully it might help others that had this problem as well.
If there are more than one inputs and you want to hide the submit, the best seems:
<input type="submit" style="visibility:hidden;position:absolute"/>
Here's the submit button style that worked for me, with minimum side-effects on the page:
.... style="width: 0px ; height: 0px" ....
So, here was our specific issue:
We had a form with multiple user input fields, but not a true <input type="submit">
(it was being submitted via JS).
As such, the GO button did nothing.
We then added an <input type="submit">
and set it to display: none
hoping that would do the trick. Nope. Didn't work.
On a whim, we changed display: none to margin-left: -1000px
That worked!
Apparently, Safari is looking for the presence of a SUBMIT button in the form and only if it's not display: none, it will then fire it when you hit the GO button.
You can also bind a keypress listener to the element or form. The iphone "Go" button is the same as the enter button on a computer, char 13.
$('someElem').bind("keypress", function(e){
// 'Go' key code is 13
if (e.which === 13) {
console.log("user pressed Go");
// submit your form with explicit JS.
}
});
GO button to submit is a default behaviour of iOS and don`t try to hack the keyboard because UIKeyboard is runtime header, however you can inject JS for your html in runtime and prevent GO button behaviour (GO acts like a Enter key),
Try this,
WKWebView *webView;
WKUserContentController *contentController = [[WKUserContentController alloc] init];
NSString *script1 = @"var isEnter = false;";
WKUserScript *userScript1 = [[WKUserScript alloc] initWithSource:script1 injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:false];
[contentController addUserScript:userScript1];
NSString *script2 = @"function captureGoKey(e){if(isEnter){e.preventDefault();}isEnter = false;}";
WKUserScript *userScript2 = [[WKUserScript alloc] initWithSource:script2 injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:false];
[contentController addUserScript:userScript2];
NSString *script3 = @"var form = document.getElementsByTagName('form')[0];";
WKUserScript *userScript3 = [[WKUserScript alloc] initWithSource:script3 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
[contentController addUserScript:userScript3];
NSString *script4 = @"document.onkeypress = function(e){if(e.keyCode == 13){isEnter = true;}}";
WKUserScript *userScript4 = [[WKUserScript alloc] initWithSource:script4 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
[contentController addUserScript:userScript4];
NSString *script5 = @"if(form.attachEvent){form.attachEvent('submit', captureGoKey);}else{form.addEventListener('submit', captureGoKey);}";
WKUserScript *userScript5 = [[WKUserScript alloc] initWithSource:script5 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
[contentController addUserScript:userScript5];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = contentController;
webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];