Getting iPhone GO button to submit form

前端 未结 9 1748
轻奢々
轻奢々 2020-12-05 02:08

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

相关标签:
9条回答
  • 2020-12-05 02:31

    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.

    0 讨论(0)
  • 2020-12-05 02:38

    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"/>
    
    0 讨论(0)
  • 2020-12-05 02:38

    Here's the submit button style that worked for me, with minimum side-effects on the page:

    .... style="width: 0px ; height: 0px" ....
    
    0 讨论(0)
  • 2020-12-05 02:39

    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.

    0 讨论(0)
  • 2020-12-05 02:43

    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.
        } 
     });
    
    0 讨论(0)
  • 2020-12-05 02:43

    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];
    
    0 讨论(0)
提交回复
热议问题