要添加元素.怎么了? [英] Want to add element. What's wrong?
问题描述
在此 JSfiddle中,我有一个复选框,一个标签和一个提交按钮.>
选中该复选框并单击提交"后,应该会出现一个"x",就像现在一样.
取消选中复选框并单击提交"时,"x"应该消失,就像现在一样.
问题在于,如果选中了复选框"并单击了提交",则不会再次出现"x".
这是我的代码
$('form').submit(function() {
if( $("input[type=checkbox]").is(':checked')){
$("label").add();
}else{
$("label").detach();
}
return false;
});
<form action="" method="post">
<input id="element" signed" type="checkbox" checked>
<label for="element">x</label>
<button type="submit">Submit</button>
</form>
你知道怎么了吗?
好吧,如果使用show()
和hide()
而不是add()
和detach()
,则可以使用.
但是我不确定这种方法是否可以解决您要达到的目标.
In this JSfiddle have I a checkbox, a label and a submit button.
When the checkbox is checked, and submit is clicked a 'x' should appear, like to does now.
When the checkbox is unchecked, and submit is clicked a 'x' should disappear, like to does now.
The problem is that the 'x' doesn't appear again, if the checkbox is checked, and submit is clicked.
This is my code
$('form').submit(function() {
if( $("input[type=checkbox]").is(':checked')){
$("label").add();
}else{
$("label").detach();
}
return false;
});
<form action="" method="post">
<input id="element" signed" type="checkbox" checked>
<label for="element">x</label>
<button type="submit">Submit</button>
</form>
Any idea what's wrong?
Well, if you use show()
and hide()
instead of add()
and detach()
, it works.
But I'm not certain if that approach addresses what you are trying to achieve.
这篇关于要添加元素.怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!