如何将以下代码转换为仅使用jquery库?
<html>
<head>
<script>
function do_it(value)
{
function newXMLHttpRequest()
{
try{ return new XMLHttpRequest(); }catch(e){}
try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
return null;
}
var ajax_request = false;
ajax_request = newXMLHttpRequest();
var url = "test.pl?b64="+value;
ajax_request.open("GET",url,1);
ajax_request.onreadystatechange = function()
{
if(ajax_request.readyState == 4)
{
var response = ajax_request.responseText;
document.getElementById("in").innerHTML = response;
}
}
ajax_request.send(null);
}
</script>
</head>
<body>
<form>
<input type="text" name="string" onkeyup="do_it(this.value)"/>
<input type="submit" name="submit">
</form>
<div style="position:absolute;width:200px;height:200px; background-color:yellow; margin-top:100px;" id="in"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
对不起,我可能应该提到我实际上没有任何关于jquery的实践经验,我现在正在熟悉它...
看看jquerys load() - 我认为这就是你要找的东西:
function do_it(value){
$('#in').load('test.pl', { b64: value });
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想拥有不同的错误和成功处理程序,使用ajax()像其他人一样发布将是要走的路 - 但是对于简单的get-this-and-put-it-into-that-div-request ,load()更简短.
EDIT2:对你的评论:最好的方法是以任何方式识别你的输入字段(给它一个id,或者给每个应该得到这个事件的字段赋予相同的类),然后简单地做:
$('#mytextfield').keyup(function(){ // with id-selector
do_it($(this).val());
});
$('.textfield').keyup(function(){ // with class-selector
// whatever
});
Run Code Online (Sandbox Code Playgroud)
(不是:我没有测试过,只是写出了我的想法......如果有问题,请看一下文档)
| 归档时间: |
|
| 查看次数: |
1167 次 |
| 最近记录: |