JQuery基于切换执行事件
我正在使用新的jQuery,并使用HTML处理它 我正在尝试基于按钮单击的JQuery基于切换执行事件,jquery,html,Jquery,Html,我正在使用新的jQuery,并使用HTML处理它 我正在尝试基于按钮单击的If-else事件 如果用户单击一个按钮,我将创建一个复选框并在下面添加一个按钮 如果用户再次单击按钮,我将删除按钮下方的复选框,如果用户再次单击,我将添加 我如何实现这一点 我在按钮单击上尝试的内容: $(document).on("click", "#button", function () { CreateCheckBox(); $("#Test").toggle("hidden"); } 但它不会
If-else
事件
如果用户单击一个按钮,我将创建一个复选框并在下面添加一个按钮
如果用户再次单击按钮,我将删除按钮下方的复选框,如果用户再次单击,我将添加
我如何实现这一点
我在按钮单击上尝试的内容:
$(document).on("click", "#button", function () {
CreateCheckBox();
$("#Test").toggle("hidden");
}
但它不会工作,因为每次都会创建一个复选框。在jQuery中执行此操作的最佳方法是什么?只需按以下方式更改代码:
$(document).on("click", "#button", function () {
$("#CheckboxID").toggle();
});
您忘记关闭“)”,并且在toggle()函数中传递了错误的参数。这里有一个JSFIDLE中的工作示例供您查看。我希望它能帮助你
$(“#我的按钮”)。单击(函数(){
if($('#复选框切换').children().length>0){
$('div#复选框切换').empty();
}否则{
$('div#复选框切换')。追加('hello')
}
});
试试这个
$(document).on('click', 'button', function () {
if ($(this).hasClass('create')) {
$('<div><input type="checkbox"><button>Delete</button></div>').appendTo('body');
// for jsfiddle test purposes I am creating the checkbox using jQuery and a button to delete
$(this).toggle(); // hides append button
} else {
$(this).parent().remove(); // removes the checkbox and delete button
$('.create').toggle(); //shows append button
}
});
$(文档)。在('click','button',函数(){
if($(this).hasClass('create')){
$('Delete').appendTo('body');
//出于JSFIDLE测试的目的,我正在使用jQuery和要删除的按钮创建复选框
$(this).toggle();//隐藏附加按钮
}否则{
$(this).parent().remove();//删除复选框和删除按钮
$('.create').toggle();//显示附加按钮
}
});
这并不完全是你想要的,但它是一个很好的选择
$(document).on('click', 'button', function () {
if ($(this).hasClass('create')) {
$('<div><input type="checkbox"><button>Delete</button></div>').appendTo('body');
// for jsfiddle test purposes I am creating the checkbox using jQuery and a button to delete
$(this).toggle(); // hides append button
} else {
$(this).parent().remove(); // removes the checkbox and delete button
$('.create').toggle(); //shows append button
}
});
$("button").on('click',function(){
$('#checkbox').toggle();
if($('#checkbox').css("display") != "none"){
$('#checkbox').hide();
}
else
{
$('#checkbox').show();
}
});