全部/无复选框

时间:2016-05-09 22:05:05

标签: javascript jquery checkbox

为什么这个“全部/无”选项不能完成他的工作?我看不出为什么count = 3没有切换所有复选框。

隐藏/显示属于所选类别的Wb元素的最聪明方法是什么?

.attr('checked', status);
#main

5 个答案:

答案 0 :(得分:2)

使用Picasso .with(context) .memoryPolicy(MemoryPolicy.NO_STORE) ... 而不是.prop()

请参阅http://api.jquery.com/prop/



.attr()

$('input[name="all"]').click(function(){ var status = $(this).is(':checked'); alert(status); $('input[type="checkbox"]').prop('checked', status); });




答案 1 :(得分:2)

如上所述here,您应该使用jQuery的.prop()函数来检查/取消选中复选框元素。

所以尝试改变你的处理程序:

int.TryParse("8", NumberStyles.Integer, System.Globalization.NumberFormatInfo.InvariantInfo, out number);

要隐藏/显示元素,我建议迭代每个元素:

$('input[name="all"]').click(function(){
    var status = !!$(this).prop('checked');
    alert(status);
    $('input[type="checkbox"]').prop('checked', status);
});

关于你关于着色的最后一个问题,我建议你使用一个类,例如$('input[type="checkbox"]').each(function() { var name = $(this).attr('name'); var status = !!$(this).prop('checked'); if (status) { $('#main').find('.' + name).show(); } else { $('#main').find('.' + name).hide(); } });

gray

答案 2 :(得分:1)

www.example.com
// Bind also the single checkboxes to show/hide the elements in div
$('input[type = "checkbox"]').click(function(){
  if($(this).prop('checked'))
    $('div.' + $(this).attr('name')).show();
  else 
    $('div.' + $(this).attr('name')).hide();
});

$('input[name="all"]').click(function(){ 
  var status = $(this).is(':checked'); 
  alert(status);    
   $('input[type="checkbox"]').each(function(){
     $(this).prop('checked', status);
     if(!status)
       $('div.' + $(this).attr('name')).hide();
     else
       $('div.' + $(this).attr('name')).show();
 }); 
});

答案 3 :(得分:1)

根据需要设置复选框和 startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; if (path.EndsWith(".cpp") || path.EndsWith(".cxx") || path.EndsWith(".cc")) { startInfo.WorkingDirectory = Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"); startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"), "g++.exe"); if (exe.EndsWith(".exe")) startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, '"') + " -Wall -Wextra -std=c++11"; else startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, ".exe", '"') + " -Wall -Wextra -std=c++11"; } if (path.EndsWith(".c")) { startInfo.WorkingDirectory = Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"); startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\MinGW64\bin"), "gcc.exe"); if (exe.EndsWith(".exe")) startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, '"') + " -Wall -Wextra -std=c11"; else startInfo.Arguments = string.Concat('"', path, '"') + " -o " + string.Concat('"', exe, ".exe", '"') + " -Wall -Wextra -std=c11"; } if (path.EndsWith(".cs")) { startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "csc.exe"); if (exe.EndsWith(".exe")) startInfo.Arguments = "/out:" + string.Concat('"', exe, '"') + " /target:exe " + string.Concat('"', path, '"'); else startInfo.Arguments = "/out:" + string.Concat('"', exe, ".exe", '"') + " /target:exe " + string.Concat('"', path, '"'); } if(path.EndsWith(".vb")) { startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "vbc.exe"); if(exe.EndsWith(".exe")) startInfo.Arguments = "/out:" + string.Concat('"', exe, '"') + " /target:exe " + string.Concat('"', path, '"'); else startInfo.Arguments = "/out:" + string.Concat('"', exe, ".exe", '"') + " /target:exe " + string.Concat('"', path, '"'); } if(path.EndsWith(".il")) { startInfo.FileName = Path.Combine(Path.Combine(Application.StartupPath, @"compiler\v4.0.30319"), "ilasm.exe"); if (exe.EndsWith(".exe")) startInfo.Arguments = string.Concat('"', path, '"') + " /EXE /OUTPUT:" + string.Concat('"', exe, '"'); else startInfo.Arguments = string.Concat('"', path, '"') + " /EXE /OUTPUT:" + string.Concat('"', exe, ".exe", '"'); } #endregion #region Launching compiler and redirect its stdout to a TextBox TCCompilerOutputOutputTextBox.Text += string.Concat(startInfo.FileName, ' ', startInfo.Arguments, "\r\n"); try { using (Process comp = new Process() { StartInfo = startInfo, EnableRaisingEvents = true } ) { try { comp.ErrorDataReceived += Comp_InterceptOutput; comp.OutputDataReceived += Comp_InterceptOutput; comp.Start(); comp.BeginErrorReadLine(); comp.BeginOutputReadLine(); comp.WaitForExit(); TCCompilerOutputOutputTextBox.AppendText(OutputBuilder.ToString()); OutputBuilder.Clear(); TimeSpan time = comp.TotalProcessorTime; var flag = !Convert.ToBoolean(comp.ExitCode); if (flag) { TCCompilerOutputOutputTextBox.AppendText("\r\n\r\nCompilation finished after " + time + "\r\n" + "--------------------------------------------------------------------------------\r\n\r\n"); } else { TCCompilerOutputOutputTextBox.AppendText("\r\n\r\nCompilation failed!\r\n" + "--------------------------------------------------------------------------------\r\n\r\n"); } } catch (Exception ex) { var bfr = new ExceptionInformer(ex); bfr.ShowDialog(); } } } catch(Exception ex) { var bfr = new ExceptionInformer(ex); bfr.ShowDialog(); } } 可见性。

它使用来模拟灰显的复选框。

div



opacity

$('[name="all"]').click(function() {  //set all checkboxes to match All / none
  $(':checkbox')
    .prop('checked', this.checked)
    .change();
});

$('input')
  .change(function() {  //show divs corresponding to checked input
    var checked= $(':checkbox:not([name="all"]):checked').length;

    $('div.' + this.name)
      .toggle(this.checked);

    $('[name="all"]')
      .prop('checked', checked > 0)
      .toggleClass('someChecked', checked && checked<$(':checkbox:not([name="all"])').length);
  })
  .change();  //run the method immediately
&#13;
$('[name="all"]').click(function() {  //set all checkboxes to match All / none
  $(':checkbox')
    .prop('checked', this.checked)
    .change();
});

$('input')
  .change(function() {  //show divs corresponding to checked input
    var checked= $(':checkbox:not([name="all"]):checked').length;
  
    $('div.' + this.name)
      .toggle(this.checked);

    $('[name="all"]')
      .prop('checked', checked > 0)
      .toggleClass('someChecked', checked && checked<$(':checkbox:not([name="all"])').length);
  })
  .change();  //run the method immediately
&#13;
&#13;
&#13;

答案 4 :(得分:1)

这里的所有答案都很棒。仅供将来参考,我发布我将使用的那个(这是@RickHitchcock和复选框indeterminate状态的混合使用):

$('input[name="all"]').click(function() { $(':checkbox').prop('checked', this.checked).change(); });
$('input[type="checkbox"]').change(function() {  
  var checked = $(':checkbox:not([name="all"]):checked').length;
  $('div.' + this.name).toggle(this.checked);
  $('input[name="all"]').prop('checked', checked > 0)
                    .prop('indeterminate', checked && checked < $(':checkbox:not([name="all"])').length);
}).change();  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="main">
  <div class="cat1">Element of category1</div>
  <div class="cat4">Element of category4</div>
  <div class="cat3">Element of category3</div>
  <div class="cat1">Element of category1</div>
  <div class="cat2">Element of category2</div>
</div>
<label><input type="checkbox" name="all" checked>All / none</label>
<label><input type="checkbox" name="cat1" checked>A</label>
<label><input type="checkbox" name="cat2" checked>B</label>
<label><input type="checkbox" name="cat3" checked>C</label>
<label><input type="checkbox" name="cat4" checked>D</label>