简体   繁体   English

如果在Sharepoint Designer上选中了复选框,如何显示div?

[英]How can I show a div if checkbox is ticked on Sharepoint designer?

I don't know why this code won't work but when I check the checkbox it won't work. 我不知道为什么此代码不起作用,但是当我选中该复选框时将不起作用。 The div just remains hidden. div只是保持隐藏状态。 I have done this before but this time it won't work. 我之前已经做过,但是这次不起作用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="WebPartPageExpansion" content="full" />
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<script type="text/javascript" src="../SiteAssets/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('input[type=checkbox]').change(function(){
        var checked = $(this).attr('checked');
        if (checked) {
           $('.other').show();             
        } else {
            $('.other').hide();
        }
    });        
});
</script>
</head>
<body>
<div name="main">

    <input type="checkbox" /> If this is checked display div
</div>
<div class='other' name='other' title='other' style='display:none;'>  
<h2>test</h2>
<input></input>
<input></input>
<input></input>
</div>
</body>
</html>   

What am I doing wrong? 我究竟做错了什么? I can't figure this out. 我不知道这个。 Thanks. 谢谢。

As there is no attribute checked in checkbox. 由于没有属性 checked的复选框。 So it is returning undefined .You can directly access the property by this.checked . 因此它返回undefined 。您可以通过this.checked直接访问属性。

  $(document).ready(function(){
        $('input[type=checkbox]').change(function(){
            var checked = this.checked;
            if (checked) {
               $('.other').show();             
            } else {
                $('.other').hide();
            }
        });        
    });

CHECK THIS : 检查一下

you are using attr that returns undefined 您正在使用返回undefined attr

  var checked = $(this).attr('checked');
    if (checked) {    //here checked is not a boolean 

Always remember 总记得

.prop('checked') returns boolean .prop('checked')返回布尔值

So use .prop() . 因此,请使用.prop() Rest is fine . 休息很好。

Hope it help :) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果选中复选框,如何显示每个表格行的div? - How to show div per table row if checkbox is ticked? 勾选复选框时隐藏 div - Hide div when checkbox is ticked 如何将复选框值(勾选或未勾选)放入 jquery/ajax 变量中? - How can I get a checkbox value (ticked or unticked) into jquery / ajax variable? 如何通过从 Javascript 中的 JSON 文件获取 boolean 值来设置已勾选的单选按钮/复选框 - How can I set the radio buttons/checkbox already ticked by getting boolean values from a JSON file in a Javascript 如何在定标中创建表示是否选中复选框的javascript变量? - How do I create a javascript variable in qualtrics that represents if a checkbox is ticked or not? 勾选后如何删除复选框? - How to delete a checkbox when it is ticked? 如果选中表单中的复选框,则将其添加到结果div类中 - If checkbox in a form is ticked add it to the resulting div class 使用jQuery检测复选框是否在目标div中被勾选 - detecting if a checkbox is ticked inside a target div with jQuery 当另一个复选框被勾选时,如何禁用该复选框,当未勾选该复选框时又如何重新启用? - How do I disable a checkbox when another checkbox is ticked & re-enable when unticked? 除非在Knockout的Sharepoint中打勾,否则为什么我无法隐藏我的JavaScript框? - Why can't I get my javascript to hide box unless ticked in Sharepoint with Knockout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM