[英]How to Append a section after clicking a radio button in JQuery?
Here's the problem. 这是问题所在。 I need to append a section after the user clicks an radio button.
用户单击单选按钮后,我需要添加一个部分。 Then I need to append another section if the use clicks the radio button in this new, appended section.
然后,如果用户单击此新附加部分中的单选按钮,则需要附加另一部分。 My code doesn't works.
我的代码无效。 I'm doing something wrong :( your help is appreciated!!!
我做错了:(感谢您的帮助!!!
<section class="input_1">
<input type="datetime-local"><br/>
<input id="add_1" type="radio" value="1" name="q1">yes
<input id="del_1" type="radio" value="0" name="q1" >no
<br />
</section>
$(document).ready(function() {
var max_fields = 2;
var x = 1;
$(document).on("click", "#add_1", function(e) {
e.preventDefault();
if (x < max_fields) {
x++;
$(".input_1").append('<section class="input_2">-.-.-<br/><input type="datetime-local"><br />
<input type="datetime-local"><br/>
<input id="add_2" type="radio" value="1" name="q2">yes
<input id="del_2" type="radio" value="0" name="q2" >no
</section>');
})
}
});
});
$(".input_1").on("click", "#del_1", function(e) {
e.preventDefault();
$(this).parent('section').remove();
x--;
})
});
//level 2
$(document).ready(function() {
var max_fields_2 = 2;
var x = 1;
$(document).on('click', '.add_2', function(e) {
e.preventDefault();
var wrapper_2 = $(".input_2");
if (x < max_fields_2) {
x++;
$(wrapper_2).append('<section>-.-.-<br/><input type="datetime-local"><a href="#" class="del_2">Remove</a><br /> <button class="add_3">return 2</button> </section>');
$(wrapper_2).on("click", ".del_2", function(e) {
e.preventDefault();
$(this).parent('section').remove();
x--;
})
}
});
});
edit Here is an alternative version of the code according to the solution offered by vlk 编辑这是根据vlk提供的解决方案的代码的替代版本
<input type="datetime-local"><br/>
<input id="add_1" type="radio" value="1" name="q1" onclick="appendNewDiv()" ><label for="add_1">yes</label>
<input id="del_1" type="radio" value="0" name="q1" onclick="removeDiv($(this))"> <label for="add_del">no</label>
<br />
<div class="container"></div>
JavaScript
var newDiv
function appendNewDiv(){
newDiv = '<div><p>This is a new div</p>';
newDiv += '<label><input onclick="appendNewDiv()" '
newDiv += 'type="radio">Add Another Div</label>'
newDiv += '<label><input onclick="removeDiv($(this))" '
newDiv += 'type="radio">Remove Div</label></div>'
$('.container').append(newDiv);
}
function removeDiv(radio){
radio.closest('div').remove()
}
Try with something of very simple like this. 尝试像这样非常简单的方法。 Use inline events call
使用内联事件调用
onclick="yourFunction()"
on appended div to make it smarter and with less code. 在附加的div上,以使其更智能,代码更少。
Create a variable for the new div like this 像这样为新的div创建一个变量
var newDiv = '<div>Hello</div>';
var newDiv; function appendNewDiv(){ newDiv = '<div><p>This is a new div</p>'; newDiv += '<label><input onclick="appendNewDiv()" '; newDiv += 'type="radio">Add Another Div</label></div>'; $('.container').append(newDiv); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input onclick="appendNewDiv()" id="radio1" type="radio"> Add Div <label> <div class="container"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.