[英]How can I get the source code of a specific div on radio oncheck?
I'd like to get the source code of a div, so example if a div contains some tags I'd like to get them as they are in html format and update it on a textfield. 我想获取div的源代码,因此,举例来说,如果div包含一些标记,则希望以html格式获取它们,并在文本字段上对其进行更新。
JsFiddle : https://jsfiddle.net/Lindow/g1sp1ms3/2/ JsFiddle: https ://jsfiddle.net/Lindow/g1sp1ms3/2/
HTML : HTML:
<form>
<label class="tp_box_1">
<input type="radio" name="selected_layout" class="selected_layout" value="layout_1">
<div class="box_1">
<h3>box_one</h3>
<p>1</p>
</div>
</label>
<br><hr><br>
<label class="tp_box_2">
<input type="radio" name="selected_layout" class="selected_layout" value="layout_2">
<div class="box_2">
<h3>box_two</h3>
<p>2</p>
</div>
</label>
<textarea id="mytextarea"></textarea>
<input id="submit_form" type="button" value="Send">
</form>
JavaScript : JavaScript :
$('input[type="radio"][name="selected_layout"]').change(function() {
if(this.checked) {
// get the specific div children of $this
var selected_layout = $(this).find('.selected_layout');
// get source code of what's inside the selected_layout
/* example :
<h3>box_two</h3>
<p>2</p>
and put it in someVariable.
*/
// and put in into textarea (all this need to happens when a radio is changed with the source code of the checked div)
var someVariable = ...
$('textarea').val(someVariable);
}
});
How can I achieve this ? 我该如何实现? How can I get the source code inside a specific div ?
如何获取特定div中的源代码?
First, you don't want selected_layout
to be equal to: $(this).find('.selected_layout')
because this
already points to that element. 首先,您不希望
selected_layout
等于: $(this).find('.selected_layout')
因为this
已经指向该元素。 You want it to point to the next element that comes after it. 您希望它指向其后的下一个元素。
I think this is what you are looking for: 我认为这是您要寻找的:
$('input[type="radio"][name="selected_layout"]').change(function() { if(this.checked) { // get the index within the set of radio buttons for the radio button that was clicked var idx = $("[type=radio][class=selected_layout]").index(this); // Get the div structure that corresponds to the same index var test = $("[class^='box_']")[idx]; // Now, just make that the value of the textarea $('textarea').val(test.innerHTML); } });
textarea { width:100%; height:75px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label class="tp_box_1"> <input type="radio" name="selected_layout" id="sl1" class="selected_layout" value="layout_1"> <div class="box_1"> <h3>box_one</h3> <p>1</p> </div> </label> <label class="tp_box_2"> <input type="radio" name="selected_layout" id="sl2" class="selected_layout" value="layout_2"> <div class="box_2"> <h3>box_two</h3> <p>2</p> </div> </label> <textarea id="mytextarea"></textarea> <input id="submit_form" type="button" value="Send"> </form>
Create div
element with: 使用以下命令创建
div
元素:
class
or id
for identifying class
或id
Set inputs' value to desired template class/id, then on input change find the template element base on input's value and extract the innerHTML of it by using jQuery's .html()
method. 将输入的值设置为所需的模板类/ id,然后在输入更改时基于输入的值找到模板元素,并使用jQuery的
.html()
方法提取其innerHTML。 Then put this HTML as an new value of textarea using also the .html()
method on textarea element. 然后,还使用textarea元素上的
.html()
方法将此HTML用作textarea的新值。
HTML: HTML:
<div id="template1" style="display:none;">
<h1>Hi!</h1>
</div>
<input type="radio" onchange="findTemplate(event)" value="template1" />
<textarea class="texta"></textarea>
jQuery: jQuery的:
var findTemplate = function(event) {
var target = $(event.target);
var templateName = target.val();
var template = $("#"+templateName);
var template = template.html();
var textarea = $(".texta");
textarea.html(template);
};
Working fiddle: https://jsfiddle.net/rsvvx6da/1/ 工作提琴: https : //jsfiddle.net/rsvvx6da/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.