我试图在我的网页浏览中实时捕捉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)
捕获转发网址
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print(request.url?.absoluteString)
var url = request.url?.absoluteString
return true
}
答案 0 :(得分:1)
我相信您的问题是使用
window.location.href = "?" + search_serialized;
调用您的代理捕获的网址,导致文本字段模糊。
您可能希望在您的网页中使用可以捕获的特殊链接(因此它不会捕获所有负载)并从代理返回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
}
因此,请确保您只捕获表单事件发布的特殊网址,并返回false,这样可以防止模糊。