开发者

jQuery replace shows up in logs/alert but not in actual content?

This seems weird, but doing a jQuery replace() seems to only work when it's run in alert() or console.log.

Here's the code I'm trying to run:

$('.settings_item[data-slide-id=1324] .answers_fields_template li').each(function(index) {
  var regexp = new RegExp('new_answers', 'g');
  var new_id = new Date().getTime();
  $(this).html().replace(regexp, new_id);
});

And here's the html code:

<li data-weight="1">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_skeleton" name="survey[questions_attributes][0][answers_attributes][new_answers][skeleton]" type="hidden" value="true">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_text" name="survey[questions_attributes][0][answers_attributes][new_answers][text]" size="30" type="text" value="Great!">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_weight" name="survey[questions_attributes][0][answers_attributes][new_answers][weight]" type="hidden" value="3">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_response_class" name="survey[questions_attributes][0][answers_attributes][new_answers][response_class]" type="hidden" value="rate">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_id" name="survey[questions_attributes][0][answers_attributes][new_answers][id]" type="hidden">
</li>
<li data-weight="2">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_skeleton" name="survey[questions_attributes][0][answers_attributes][new_answers][skeleton]" type="hidden" value="true">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_text" name="survey[questions_attributes][0][answers_attributes][new_answers][text]" size="30" type="text" value="OK">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_weight" name="survey[questions_attributes][0][answers_attributes][new_answers][weight]" type="hidden" value="2">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_response_class" name="survey[questions_attributes][0][answers_attributes][new_answers][response_class]" type="hidden" value="rate">
    <input id="survey_questions_attributes_0_answers_attributes_new_answers_id" name="survey[questions_attributes][0][answers_attributes][new_answers][id]" type="hidden">
</li>

I'm trying to replace all instances of new_answers with the contents of the new_id variable.

When I run that, nothing gets replace. But, if I wrap $(this).html().replace(regexp, new_id); in alert() or console.log(), then the output of those shows new_answers being replaced as it should.

I can't figure out w开发者_如何学编程hy on earth that'd be the case and now my brain hurts.


You need to give the replaced HTML back to the element. Try this line:

$(this).html($(this).html().replace(regexp, new_id));

James


Are you trying to do a .replaceWith()? I do not see .replace() in the jquery API.


var newHtml = $(this).html().replace(regexp, new_id);
$(this).html(newHtml);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜