0

我一直无法让基于 jQuery 的 POST 请求正常工作。

第 20 行抛出此错误:

Uncaught SyntaxError: Unexpected identifier 。

这些按钮是用 PHP 生成的,如下所示:

<a id="df-save-218281" 
  class="button js-saved-search"
  href="/realestate/addsearch/?s=-List+Price&amp;Town_State=Burlington+VT&amp;Description%5Blikeany%5D=five+sisters&amp;State=VT&amp;Status%5Bany%5D=ACT%2CUAG%2CPCG%2CPEN%2CRNT%2CA%2CK%2CL%2CO%2CR%2CT"
  data-alt-href="/realestate/removesearch/218281/"
  data-alt-text="Delete Search"
  data-toggle-class="remove">
  <span>Save Your Search</span>
</a>

Javascript在这里:

$(function () {
  $('.js-saved-search').click(function(event) {
    console.log('clicked');
    event.preventDefault();

    var jsSavedSearch = $(this),
      queryString = decodeURIComponent(jsSavedSearch.attr('href')),
      url = jsSavedSearch.attr('href'),
      name = jsSavedSearch.text();
      id = jsSavedSearch.attr('id').replace(/\D/g, '');

      console.log(id);
      console.log(name);
      console.log(queryString);

    var posting = $.post( queryString, { SearchName: name } );

    posting.done(function( data ) {
      console.log(data);
      console.log(jsSavedSearch.attr('data-alt-text').replace(/\/\d+\//g, data));
      console.log(jsSavedSearch.text());
      console.log(jsSavedSearch.attr('data-alt-href'));
      console.log(jsSavedSearch.attr('href'));

      newText = jsSavedSearch.attr('data-alt-text');
      newAltText = jsSavedSearch.text();
      newHref = jsSavedSearch.attr('data-alt-href');
      newAltHref = jsSavedSearch.attr('href');

      console.log(newAltText);
      console.log(newText);
      console.log(newAltHref);
      console.log(newHref);

      jsSavedSearch.attr('data-alt-text', newAltText);
      jsSavedSearch.text(newText);
      jsSavedSearch.attr('data-alt-href', newAltHref);
      jsSavedSearch.attr('href', newHref);

    });
  })
});

编辑: 问题似乎出在这个正则表达式上。我无法弄清楚如何在 JavaScript 正则表达式中转义值。

newText = jsSavedSearch.attr('data-alt-text').replace(/\/\d+/\/g, data)

4

1 回答 1

2

看起来你;在定义变量时已经代替了,..

var jsSavedSearch = $(this),
  queryString = decodeURIComponent(jsSavedSearch.attr('href')),
  url = jsSavedSearch.attr('href'),
  name = jsSavedSearch.text();
  id = jsSavedSearch.attr('id').replace(/\D/g, '');

应该

var jsSavedSearch = $(this),
  queryString = decodeURIComponent(jsSavedSearch.attr('href')),
  url = jsSavedSearch.attr('href'),
  name = jsSavedSearch.text(), 
                      //-----^---here
  id = jsSavedSearch.attr('id').replace(/\D/g, '');
于 2013-06-24T18:58:18.603 回答