【问题标题】:Is there any way to capture the android 'GO' button or iPhone 'GO' button in jquery?有没有办法在 jquery 中捕获 android 'GO' 按钮或 iPhone 'GO' 按钮?
【发布时间】:2012-06-29 05:54:57
【问题描述】:

我正在构建一个移动网站,在这个表单中,没有提交按钮。因此,当用户在输入字段中键入内容并单击 Android 或 iPhone GO 按钮时,它应该提交表单。我为此使用 HTML 和 jQuery。

如何在 jQuery 中捕获这个 GO 事件?我希望它自己以这种方式完成。

【问题讨论】:

    标签: android jquery iphone html form-submit


    【解决方案1】:

    添加一个提交按钮并将其opacity 设置为0

    我把这个用于html:

    <form>
        <input type="text" name="input" />
        <input type="submit" style="opacity:0;" />
    </form>
    

    还有这个给alert()的值:

    $(document).ready(function() {
        $("form").submit(function() {
            alert($("[name=input]").val());
        });
    });
    

    您只需要处理表单的提交事件,这对 jQuery 非常有用。

    【讨论】:

      【解决方案2】:

      您可以使用 webview 在 android/iphone 设备中加载 html 文件。

      检查以下,

      1. 在android的assets文件夹下添加index.html。并在 mainActivity 类中添加以下内容。这里命名为 StackOverFlowActivity

        import android.app.Activity;
        import android.os.Bundle;
        import android.os.Handler;
        import android.webkit.WebView;
        
        public class StackOverFlowActivity extends Activity {
        
        private Handler mHandler = new Handler();
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            WebView view=(WebView)findViewById(R.id.webView1);
            view.getSettings().setJavaScriptEnabled(true);
            view.loadUrl("file:///android_asset/index.html");
            view.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
        }
        
        final class MyJavaScriptInterface
        {
            public void ProcessJavaScript(final String scriptname, final String args)
                {             
                    mHandler.post(new Runnable()
                        {
                            public void run()
                                {
                                    String h="Hi";
                                    webview.loadUrl("javascript:result(\""+h+"\")");
                                }
                        });
                }
        }
        

        }

      2. index.html

        <html>
        <head>
        <script type="text/javascript">
        function clickhere()()
        {
           Android.ProcessJavaScript("result",null); 
        }
        
        function result(res) 
        {
        
            alert(res);       
         }
         </script>
         </head>
        
         <body>
         <input type="button" id="clickme" onclick="clickhere()" value="GO"></input>
         </body>
         </html>
        

      【讨论】:

      • 这是一个很酷的方式,但他正在开发一个移动网站,而不是一个应用程序 ;-)
      • 我试图将其标记为“不是答案”,因为它与所提出的问题无关,即使它对另一个问题有帮助。不幸的是,这个标志被评论者质疑,所以我投反对票,希望它可能会被海报删除。道歉!
      猜你喜欢
      • 1970-01-01
      • 2013-07-07
      • 2010-09-13
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多