从Web窗体创建代码片段(易于剪切和粘贴)[关闭]
问题描述:
I'd like a staff of non-developers to be able to enter text into 4-5 fields in a form (date, time, price, description) then have a box at the bottom of the page that shows custom HTML code with their answers automatically embedded, so they can cut-and-paste the HTML.
Alternatively, send the form to a second page but display it as HTML source code HTML that they can Select All, Cut, then Paste into another app.
Thanks.
text [form field]
name: [bob]
price: [50]
description: [come to our event]
url for event: [url.com/event-02]
<html>
<p class="name">bob</p>
<p class="price">50</p>
<p class="desc">come to our event</p>
<p class="url">Share the <a href="url.com/event-02">event with your friends</a></p>
</html>
This way they don't have to mess with HTML.
答
something like this, using val()
to get the input from the user:
HTML:
name:<input type="text" id="name" />
<br />
price:<input type="text" id="price"/>
<br />
description:<input type="text" id="description"/>
<br />
url for event:<input type="text" id="url_for_event" />
<br />
<button id="create-html">create html</button>
<div id="result"></div>
JS:
$( "#create-html" ).click(function() {
var name = '<p class="name">' + $("#name").val() + '</p>';
var price = '<p class="price">' + $("#price").val() + '</p>';
var description = '<p class="desc">' + $("#description").val() +'</p>';
var url_for_event = '<p class="url">Share the <a href="' + $("#url_for_event").val() +'">event with your friends</a></p>';
$( "#result" ).text(name + price + description + url_for_event);
});
答
Try making the submit button trigger a JS function.
<form action="">
<!-- your form -->
<input type="submit" value="Submit" onclick="parseThisCrapIntoHtml(this);return!1" />
</form>
Now, parsing into HTML should be relatively straightforward, mostly comprised of string manipulation.