[英]Create HTML element using Form Input value via jQuery
I am trying to implement automatically HTML using form value. 我正在尝试使用表单值自动实现HTML。
When user enter the information in the form, the JS will create the correspond HTML for him. 当用户在表单中输入信息时,JS将为他创建相应的HTML。
For example. 例如。
<div class="row" id="1">
<div class="form-group col-lg-2">
<select class="form-control" name="tag">
<option value="p" selected>p</option>
<option value="br">br</option>
</select>
</div>
<div class="form-group col-lg-2">
<select class="form-control" name="class">
<option value="Day" selected>Day</option>
<option value="BlockTime">BlockTime</option>
<option value="BlockTitle">BlockTitle</option>
<option value="Session">Session</option>
<option value="Person">Person</option>
<option value="Firm">Firm</option>
</select>
</div>
<div class="form-group col-lg-7">
<textarea class="form-control" rows="3" name="content"></textarea>
</div>
<div class="form-group col-lg-1">
<input type="button" class="btn btn-default" onclick="addLine()" value="Add">
</div>
</div>
The user selected P, Day, Test message, and the press "Add" button, the button will call function addLine()
. 用户选择了P,Day,Test消息,然后按“添加”按钮,该按钮将调用函数addLine()
。 The main part is below, I didn't make it right. 主要部分在下面,我没有弄对。
var currentTag = currentLine.find("select[name='tag'] option:selected").text();
var currentClass = currentLine.find("select[name='class'] option:selected").text();
var currentText = currentLine.find("textarea").val();
var new_content = $("<currentTag></currentTag>").addClass(currentClass).html(currentText);
Now the currentTag
will get the value "p", currentClass
will get "Day", currentText
do get "Test message", this has been done. 现在currentTag
将获得值“ p”, currentClass
将获得“ Day”, currentText
得到“测试消息”,此操作已完成。
This is what I want, the jQuery creates HTML code like this: 这就是我想要的,jQuery创建这样的HTML代码:
<p class="Day">Test message</p>
And I want to store the HTML code to a variable called new_content
. 我想将HTML代码存储到名为new_content
的变量中。 ... ...
If you have a container for these elements, you could just append them to it: 如果您有这些元素的容器,则可以将它们附加到该容器:
First you want to set your new line as a variable: 首先,您要将新行设置为变量:
var newElements = [];
var i = 0; //Make an index counter so you can use it later if you want to. This is optional
/* Do your selection script */
var htmlElement = "<" + currentTag + " class='" + currentClass + "'>" + currentText + "</" + currentTag + ">";
newElement.push(htmlElement, i);
$('.container').append(htmlElement);
i++; //increment your i to create an index
/* Make sure all of this is inside your selection script. */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.