这个jQuery有什么问题吗?

时间:2010-09-14 02:11:15

标签: jquery html url get

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一样。如果你知道我的意思吗?

3 个答案:

答案 0 :(得分:4)

我没有测试整个脚本函数,但是你需要删除标记和类/ ID之间的空格,所以使用“a.reply”和“input#inputField”(或只是“#inputField”) :

$("a.reply").click(function () {
   insertParamIntoField(this.href, "replyto", $("#inputField")[0]);
   return false; // prevent default action
});

答案 1 :(得分:1)

我在这里作弊:)

function insertParamIntoField(url, param, field) { 
   var anchor = document.createElement('a'), query;
   anchor.href = url;
   query = anchor.search.substring(1, anchor.search.length).split('&');

   for(var i = 0, kv; i < query.length; i++) {
      kv = query[i].split('=', 2); console.log(kv);
      if (kv[0] == param) {
         field.value = kv[1];
         return;
      }
   }  
}

$("a.reply").click(function () {
   insertParamIntoField(this.href, "replyto", $("textarea.inputField")[0]);
   return false; // prevent default action
});

或者:

function insertParamIntoField(anchor, param, field) { 
   var query = anchor.search.substring(1, anchor.search.length).split('&'); // anchor is a DOMElement 

   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, "replyto", $("textarea.inputField")[0]);
   return false; // prevent default action
});

答案 2 :(得分:1)

除了fudgey建议的内容之外,我还注意到有关insertParamIntoField功能的一些事项:

// url: home.php?replyto=username, param: replyto, field: $("input .inputField")[0]
function insertParamIntoField(url, param, field) {  
   var anchor = document.createElement('a'), query; 
   // anchor is a link element and doesn't have a 'value' attribute
   anchor.value = url; 
   // It also won't have a 'query' attribute. This probably causes an error
   query = anchor.query.split('&'); 

   // As stated above there is no 'query' attribute to split on, but
   // also there are no &'s to split on in the url you pass in anyway.
   // At best, query will equal the full url...
   for(var i = 0, kv; i < query.length; i++) { 
      kv = query[i].split('=', 2); 
      // ...so splitting on = would get you 
      // query[0] = "home.php?replyto", query[1] = "username"
      // ... and "home.php?replyto" != "replyto", so...
      if (kv[0] == param) { 
         //... it never gets here. Also 'field' is a textarea, not an input
         // so it also doesn't have a 'value' attribute. try field.html(kv[1]) istead
         field.value = kv[1]; 
         return; 
      } 
   }   
} 

试试这个:

function insertParamIntoField(url, param, field) {
   var qs = url.split("?");
   var query = qs[1].split("&");

   for(var i = 0, kv; i < query.length; i++) {
      kv = query[i].split('=', 2);
      if (kv[0] == param) {
         field.html(kv[1]);
         return;
      }
   }  
}

$("a.reply").click(function () {
   insertParamIntoField(this.href, "replyto", $("textarea#inputField"));
   return false; // prevent default action
});