【问题标题】:Is there something wrong with this jQuery?这个 jQuery 有什么问题吗?
【发布时间】:2010-09-14 02:11:14
【问题描述】:
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 一样。如果你明白我的意思吗?

【问题讨论】:

  • 您能告诉我们发生了什么或出现什么错误消息吗?
  • 什么都没有发生,或者没有显示错误,它只是没有做它应该做的事情?
  • 尝试使用 Firebug 或 Chrome 开发者工具逐步完成...查看每个参数/变量的确切设置。

标签: jquery html url get


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我有点作弊:)

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

    【讨论】:

      【解决方案3】:

      除了 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
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-08
        • 1970-01-01
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多