2013-08-26 2 views
1

<strong>Title</strong> 태그를 모두 <input type="text" value="Title"><p>Content</p> 태그를 <textarea>Content</textarea> 태그로 변경하는 프로필 페이지를 만들고 있습니다.정규식 : 데이터 속성 전송

이이와 함께 잘 작동 : (그들은 프로파일의 다른 부분을 대표로)

$(this).html(function(i, h) { 
    return h 
    .replace(/<strong>/g, "<input type=\"text\" value=\"") 
    .replace(/<\/strong>/g, "\" class=\"profile-header\">") 
    .replace(/<p>/g, "<textarea>") 
    .replace(/<\/p>/g, "</textarea>"); 
}); 

는 그러나, 나는 <strong><p> 태그의 각 세트에 대한 ID가 필요합니다. 내가 데 문제는 내가이 같은 <strong> 태그에 넣어 가지고 이들에 대한 ID, 추가하고있다 : <strong data-id="0">를하지만 사용하고 정규 표현식이 제대로 작동하지 않는 이유를 작동하지 않을 수 있습니다. 여기

내가 잘하면 당신은 내가 달성하기 위해 노력하고있어 볼 수 있습니다, 지금까지이 작업은 다음과 같습니다

$(this).html(function(i, h) { 
    return h 
    .replace(/<strong data-id=\"(\i+)\">/g, "<input $1 type=\"text\" value=\"") 
    .replace(/<\/strong>/g, "\" class=\"profile-header\">") 
    .replace(/<p>/g, "<textarea>") 
    .replace(/<\/p>/g, "</textarea>"); 
}); 

그리고 단지 참조를,이 내가 원래 상태로 다시 HTML을 반환 (그리고 내가 얼마나입니다 데이터가) 저장 방법 :

여기
$(".column.about input").each(function() { 

    // Get the titles and content from the inputs and textareas 
    var content_id = $(this).data("id"); 
    var title = $(this).val(); 
    var content = $(this).next().val(); 

    // We're changing the content first so we don't loose $(this) 
    $(this).next().after("<p>" + content + "</p>").remove(); 
    $(this).after("<strong data-id=\"" + content_id + "\">" + title + "</strong>").remove(); 

    $.post("../includes/ajax.php", { action: "updateProfile", section: "about", id: content_id, title: title, content: content }).done(function(data) { 

     if(data != "saved") { 

     throwErrorMessage(data); 

     } 

    }); 

}); 
+1

작업하기 쉬울 수 있습니다 만약 당신이'.each'' "강력한"'와' "P"'- 당신은 쉽게 사용에 의해 .attr''와 속성을 전송 할 수 있습니다 - 다음 insertBefore'은'사용 (원본을 숨기고 처리 textarea'은'입력에 클래스 /의 예'.editInput + P, .editInput + 강력한 {디스플레이 : 없음;}' – SmokeyPHP

+2

[그래,/구문 분석 정규식 HTML을 조작하려고하지 않는다] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags는) – naomik

+0

그래서 내 가장 좋은 방법은 정규식 사용을 중지하고 ((함수를'.each를 복사하는 것입니다) {'이전에 다시 사용 했습니까? –

답변

0

내가) 정규 표현식없이 (한 일이다 :

$(".column.about strong").each(function() { 

    // Get the titles and content from the strong and p tags 
    var content_id = $(this).data("id"); 
    var title = $(this).text(); 
    var content = $(this).next().text(); 

    // We're changing the content first so we don't loose $(this) 
    $(this).next().after("<textarea>" + content + "</textarea>").remove(); 
    $(this).after("<input type=\"text\" data-id=\"" + content_id + "\" value=\"" + title + "\">").remove(); 

}); 

그리고 그것은 작동합니다. 행복한 날들.