function insertParamIntoField(url, param, field) {
var anchor = document.createElement('a'), query;
anchor.value = url;
query = anchor.query.split('&');
for(var i = 0, kv; i < query.length; i++) {
kv = query[i].split('=', 2);
if (kv[0] == param) {
field.value = kv[1];
return;
}
}
}
$("a .reply").click(function () {
insertParamIntoField(this.href, "replyto", $("input .inputField")[0]);
return false; // prevent default action
});
这是我的 HTML 代码:
<textarea name="inputField" id="inputField" tabindex="1" rows="2" cols="40"></textarea>
<a class ="reply" href="home.php?replyto=username">reply</a>
我想做的是使用 Ajax 将回复 URL 中的值插入到文本框中,就像 Twitter 一样。如果你明白我的意思?
最佳答案
我还没有测试整个脚本功能,但是你需要删除标签和类/ID 之间的空格,所以使用“a.reply”和“input#inputField”(或者只是“#inputField”) :
$("a.reply").click(function () {
insertParamIntoField(this.href, "replyto", $("#inputField")[0]);
return false; // prevent default action
});
关于jquery - 这个 jQuery 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3705689/