2009-08-11 20 views
1

データベースから入力されるカスタム属性があります。この属性は、属性内にJqueryが埋め込まれています。

$('#MY_DESC').val($(recdata).attr('MYATT')); 

MY_DESCがテキストフィールドである、私はこのようなフィールドに、この属性をコピーするためにjqueryのを使用して私のコードでは、いくつかの橋では、このような埋め込まれた単一引用符、

MYATT='Tony\'s Test' 

を含めることができますダイアログボックス。私は、ダイアログボックスを表示するとき、私はフィールドで見るすべては、私が見に必要なもの

トニー\

で、私はこの問題を解決するにはどうすればよい

トニーのテスト

です私は全体の文字列を見ることができますか?

答えて

2

試してみてください。

MYATT='Tony&#x27s Test' 

私は、HTMLの仕様でこれを検証する気にしませんでしたが、wikipediaエントリは言う:

あなたはしません場合は

The ability to "escape" characters in this way allows for the characters < and & (when written as &lt; and &amp;, respectively) to be interpreted as character data, rather than markup. For example, a literal < normally indicates the start of a tag, and & normally indicates the start of a character entity reference or numeric character reference; writing it as &amp; or &#x26; or &#38; allows & to be included in the content of elements or the values of attributes. The double-quote character ("), when used to quote an attribute value, must also be escaped as &quot; or &#x22; or &#34; when it appears within the attribute value itself. The single-quote character ('), when used to quote an attribute value, must also be escaped as &#x27; or &#39; (should NOT be escaped as &apos; except in XHTML documents) when it appears within the attribute value itself. However, since document authors often overlook the need to escape these characters, browsers tend to be very forgiving, treating them as markup only when subsequent text appears to confirm that intent.

+0

それはうまくいきました。 –

0

テキストフィールドの値を設定する前に、文字列に対して正規表現を実行して、文字列からすべてのバックスラッシュを削除してみてください。

+0

バックスラッシュを削除できますが、埋め込み引用符が必要です。 –

1

二重引用符を使用し、それらにカスタム属性を入れてください:) もしそうでなければ、値をエスケープすることをお勧めします。

0

あなたがこれを行う場合:

のアラート($(recdata).ATTR( 'MYATTを'));

"Tony \"と同じ結果が表示されます。これは、値がブラウザで正しく消費されていないことを意味します。この場合、エスケープされた\ '値は機能しません。

これらの値を作成中に編集する手段はありますか?レンダリングされる前にそれらを解析してエスケープ値を含めることはできますか?

関連する問題