【发布时间】:2014-02-07 15:15:37
【问题描述】:
可能是一个愚蠢的问题,但它仍然存在。我很难将 html 代码转换为纯文本,以便最好在 textarea 表单字段中查看,但 div 可以。
这是我发现的:
html:
<div class="box-checkbox-color">
<input type="checkbox" name="Color:_Green" id="sku0001-green" value="Yes" />
<label for="sku0001-green"></label>
<div style="display:none;" title="#" class="rawHTML-code-insert">This is where I would like to display raw HTML code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted</div>
</div>
<div class="box-checkbox-color">
<input type="checkbox" name="Color:_Green2" id="sku0001-green2" value="Yes" />
<label for="sku0001-green2"></label>
<div style="display:none;" title="#" class="rawHTML-code-insert">This is where I would like to display raw HTML code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted into a form field to be sent to another .php page</div>
</div>
<ul class="result"></ul>
Javascript:
$('input[type="checkbox"]').click(function(){
var title = $(this).closest('.box-checkbox-color').find('.rawHTML-code-insert').html();
// If the checkbox is checked, add the item to the ul.
if($(this).attr('checked')){
var html = '<li title="' + title + '">' + title + '</li>';
$('ul.result').append(html);
} else {
// if the checkbox is unchecked, remove the item from the ul.
$('li[title="' + title + '"]').remove();
}
});
我需要完成的是在用户选中特定复选框时显示/生成特定代码块,但我希望该代码可以作为代码读取,而不是作为标记读取并执行.重点是提供代码,以便用户可以复制和粘贴它,并对 div 名称、ID、值等进行修改……如果他们愿意的话。当然,每个复选框都会出现不同的代码,并在下面的 ul 类中一起列出。我提供了一个 js fiddle 来说明我需要什么。
我提供了一个js fiddle 示例来说明我在说什么。
【问题讨论】:
-
使用 jQuery
.text()将代码放入 textarea。 -
你能不能把它展示在 jsfiddle 中。我知道这很麻烦,但这会让我的妻子感觉更好,哈哈。
标签: javascript html forms input checkbox