Javascript 选中复选框后变量不更新
好的,我有一个基本的复选框列表,如下所示:Javascript 选中复选框后变量不更新,javascript,php,jquery,ajax,checkbox,Javascript,Php,Jquery,Ajax,Checkbox,好的,我有一个基本的复选框列表,如下所示: <div class="control-group"> <div class="controls"> <label class="checkbox"> <input type="checkbox" name="lowercase" id="lowercase" checked="true"> lowercase letters
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="lowercase" id="lowercase" checked="true"> lowercase letters
</label>
<label class="checkbox">
<input type="checkbox" name="uppercase" id="uppercase"> UPPERCASE LETTERS
</label>
<label class="checkbox">
<input type="checkbox" name="digits" id="digits"> Digits 0 to 9
</label>
<label class="checkbox">
<input type="checkbox" name="symbols" id="symbols"> Punctuation / Symbols <small>(e.g. % $ & * ! ... ) </small>
</label>
<label class="checkbox">
<input type="checkbox" name="ascii" id="ascii"> All ASCII Characters
</label>
</div>
</div>
if (isset($_POST['lowercase'])) {
$numChars += 26;
}
if (isset($_POST['uppercase'])) {
$numChars += 26;
}
if (isset($_POST['digits'])) {
$numChars += 10;
}
if (isset($_POST['symbols'])) {
$numChars += 12;
}
if (isset($_POST['ascii'])) {
$numChars = 96;
}
因此,正如您将看到的,如果用户选择了多个复选框,它将在var numChars中添加相应的值。如果用户选择“All ASCII”,那么var应该有一个固定值96,因为这是我感兴趣的可用ASCII字符数。如果勾选了“ascii”框,我将使用本论坛其他地方的代码删除其他框中的复选框
$(function () {
var el = $('input:checkbox');
el.on('change', function (e) {
if ($(this).attr('id') != 'ascii') {
if ($(this).is(':checked'))
$('#ascii').prop('checked', false);
else {
var l = $(':checkbox')
.filter(':checked')
.not('#ascii').length;
if (l == 0)
$('#ascii').prop('checked', true);
}
} else {
if ($(this).is(':checked'))
el.not($(this)).prop('checked', false);
}
});
});
一切都很好。除了一部分。如果我选择“小写”,那么numChars是26。如果我选择大写,那么numChars正确地是52。等等。如果我选择“ascii”,numChars将变为96。但是如果我选择了其他任何东西,比如“大写”numChars保持在96,尽管屏幕上的复选框在更新。它只在我选择“ascii”后发生,因此无论我在它之后按什么,它都需要再次单击来更新numChars的值
希望这是有意义的。我试图制作一个JSFIDLE,但尽管阅读了/echo/html示例,但还是在ajax方面遇到了困难 根据您的要求,您必须将操作代码更改为:
$numChars = 0;
if (isset($_POST['ascii'])) {
$numChars = 96;
}
else
{
if (isset($_POST['lowercase'])) {
$numChars += 26;
}
if (isset($_POST['uppercase'])) {
$numChars += 26;
}
if (isset($_POST['digits'])) {
$numChars += 10;
}
if (isset($_POST['symbols'])) {
$numChars += 12;
}
}
明白了
真不敢相信这么简单。。。他们不都是吗
我有这个密码:
// submit the form when boxes ticked
$(':checkbox').change (function () {
$("#pForm").submit();
});
在功能上方:
$(function(){
var el = $('input:checkbox');
el.on('change', function(e){
if($(this).attr('id')!='ascii')
{
if($(this).is(':checked'))
$('#ascii').prop('checked', false);
else
{
var l = $(':checkbox')
.filter(':checked')
.not('#ascii').length;
if (l == 0)
$('#ascii').prop('checked', true);
}
}
else
{
if ($(this).is(':checked'))
el.not($(this)).prop('checked', false);
}
});
}))
把它移到下面就不同了 你到底想要什么?如果选中了
ascii
,则其他选项不应影响,不是吗?是的,该选项已起作用,因此如果选择了ascii,则numChars为96。但是,如果在选择ascii后我改变主意,选择“小写”或其他什么,numChars会因为某种原因一直保持在96,直到我再次单击为止。在选择所有ascii后选择小写时,您期望的是96+26或26?您是否放置了标记?谢谢kumar_v,但这并不是说实话。也许在我的代码中还有其他的东西,我会尝试设置一个提琴