English 中文(简体)
难道这种 j子有错吗?
原标题:Is there something wrong with this jQuery?
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
});

这是我的超文本:

<textarea name="inputField" id="inputField" tabindex="1" rows="2" cols="40"></textarea>
<a class ="reply"  href="home.php?replyto=username">reply</a>

我试图做的是,在使用Ajax像Twitter一样的文本箱中添加“URL”的答复的价值。 如果你知道我指的是什么?

最佳回答

here:

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
});
问题回答

我先测试了整个文字功能,但你需要删除标签和类别/ID之间的空间,这样就使用“a.reply”和“投射场”(或只是“投射场”):

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

除法官建议外,我还通知你<条码>sertParamInto Field:

// 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
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签