JQuery заменить отображается в журналах / предупреждениях, но не в реальном содержании? - PullRequest
0 голосов
/ 15 июня 2011

Это кажется странным, но выполнение jQuery replace(), похоже, работает только тогда, когда оно запускается в alert() или console.log.

Вот код, который я пытаюсь запустить:

$('.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);
});

А вот HTML-код:

<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>

Я пытаюсь заменить все экземпляры new_answers содержимым переменной new_id.

Когда язапустить это, ничто не получает заменить.Но если я оберну $(this).html().replace(regexp, new_id); в alert() или console.log(), то на выходе этих шоу new_answers будет заменено, как и должно быть.

Я не могу понять, почему на земле этобыть так, и теперь мой мозг болит.

Ответы [ 3 ]

3 голосов
/ 15 июня 2011

Вам необходимо вернуть HTML-код replace d элементу. Попробуйте эту строку:

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

Джеймс

0 голосов
/ 15 июня 2011
var newHtml = $(this).html().replace(regexp, new_id);
$(this).html(newHtml);
0 голосов
/ 15 июня 2011

Вы пытаетесь сделать .replaceWith()? Я не вижу .replace() в API jquery.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...