【问题标题】:Swift IOS capture html form input in realtime jquerySwift IOS在实时jquery中捕获html表单输入
【发布时间】:2017-03-07 03:38:25
【问题描述】:

我正在尝试在我的 web 视图中实时捕获 html 表单中的文本输入。我目前正在使用 jquery 来捕获文本输入更改,然后通过我截获的 url 转发该值。我能够捕获文本输入中的每个更改,但我遇到的问题是我的文本输入失去了对输入的每个新字符的关注,我必须重新单击文本输入才能继续每次输入(window.location.href 导致它每次触发时都会失去焦点)。我使用以下代码:

var form = "
<form class="search_extended">
<input style="" type="text" name="name" id="search_field">
<input type='hidden' name='page' value='name_search'>
</form>"

var script = "
<script type="text/javascript">
$(document).ready(function() { 
$("form input, form textarea").on("input propertychange change", function() {
var search_serialized = $(".search_extended").serialize();
window.location.href = "?" + search_serialized; 
});});</script>"

script = script + "<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>"

self.webView_Search_Extended.loadHTMLString(form + script, baseURL: nil)

捕获转发 URL

   func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    print(request.url?.absoluteString)
    var url = request.url?.absoluteString
return true
}

【问题讨论】:

    标签: ios swift uiwebview


    【解决方案1】:

    我相信你的问题是通过使用

    window.location.href = "?" + search_serialized;

    调用您的代理捕获的 url,导致文本字段模糊。

    您可能希望在网页中使用可以被捕获的特殊链接(因此它不会捕获所有负载)并为此从委托返回 false:

    var form = "
    <form class="search_extended">
    <input style="" type="text" name="name" id="search_field">
    <input type='hidden' name='page' value='name_search'>
    </form>"
    
    var script = "
    <script type="text/javascript">
    $(document).ready(function() { 
    $("form input, form textarea").on("input propertychange change", function() {
    var search_serialized = $(".search_extended").serialize();
    window.location.href = "my-form-scheme://" + search_serialized;
    });});</script>"
    
    script = script + "<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>"
    
    self.webView_Search_Extended.loadHTMLString(form + script, baseURL: nil)
    

    然后是委托:

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if request.url?.scheme == "my-form-scheme" {
            print(request.url?.absoluteString)
            var url = request.url?.absoluteString
            return false // this is the important bit
        }
        return true
    }
    

    所以你确保你只捕捉到你的表单事件发布的特殊 url,并返回 false,这有望防止模糊。

    【讨论】:

    • 你是生活的品味者!您知道使用自定义方案为什么不会导致模糊的原因吗?我有点不明白为什么它会这样工作。
    • @user2423476 这实际上不是这里的自定义方案,自定义方案只是让捕获特定调用变得容易(我不确定你是否也会有来自 webview 的其他调用,而且它使答案更适用于可能遇到它的任何其他用户)。与众不同的一点是return false,在那个条件内。当您返回 true 时,您是在告诉 webview 可以加载请求,在这种情况下是重新加载页面,这反过来会模糊页面上的所有输入。返回 false 可以让您捕获数据,但不能重新加载。
    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2015-06-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2020-04-02
    • 2016-11-26
    • 1970-01-01
    相关资源
    最近更新 更多