我一直无法获得基于jQuery的POST请求。
第20行引发此错误:
Uncaught SyntaxError:意外的标识符。
按钮是用PHP生成的,如下所示:
<a id="df-save-218281"
class="button js-saved-search"
href="/realestate/addsearch/?s=-List+Price&Town_State=Burlington+VT&Description%5Blikeany%5D=five+sisters&State=VT&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)
答案 0 :(得分: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, '');