加载页面时设置复选框

时间:2012-08-14 14:00:27

标签: jquery asp.net-mvc-3

我尝试设置复选框,无法使用简单的代码。

有任何线索吗?

谢谢!

@{
    ViewBag.Title = "Web Reports - Peliculas";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
$(document).ready(function () {

        $("input[type='checkbox']").each(function(){
            var name = $(this).attr('name'); // grab name of original
            var value = $(this).attr('value'); // grab value of original

            alert(name); // It doesn't work at all :(
        });
    })​;
});
</script>

===================更新=========================== ==

生成的代码

<!DOCTYPE html>
<html>
<head>
    <title>MySite</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />

    <link href="/Scripts/jquery-ui-1.8.18/css/flick/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-ui-1.8.18/js/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-ui-1.8.18/js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-ui-1.8.18/development-bundle/ui/i18n/jquery.ui.datepicker.js"   type="text/javascript"></script>

      <script src="/Scripts/jquery-ui-1.8.18/development-bundle/ui/i18n/jquery.ui.datepicker-es.js" type="text/javascript"></script>



    <script type="text/javascript">
        $(function () {
            $('.datepicker').datepicker({
            });
        });

        $(document).ready(function () {
            $("#startDate").keyup(function () {
                $('#endDate').val($('#startDate').val());
            });
        });


    </script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div class="user">
                    <span style="text-align: right;">Bienvenido, <strong>mobile
                    </strong>! <a href="/Home/LogOff">LogOff</a></span>
            </div>
            <div id="title" style="display: table-row; vertical-align: top;">
                <div style="display: table-cell; vertical-align: top;">
                    <a href="/">
                        <img src="/Images/b-default.jpg" width="370px"  alt=""  /></a>
                </div>
                <div style="display: table-cell; vertical-align: middle; width: 100%;">
                    <h1 style="text-align: right;">
                        Web Reports</h1>
                </div>
            </div>
        </div>
        <div id="main">
            <div class="clear">
            </div>
            <script type="text/javascript">
    $(document).ready(function () {


        $("input[type='checkbox']").each(function(){
            var name = $(this).attr('name'); // grab name of original
            var value = $(this).attr('value'); // grab value of original

            alert(name);
        });


})​;
    });
</script>
<h2>

1 个答案:

答案 0 :(得分:3)

您的代码中存在错误的}),请尝试以下操作:

$(document).ready(function () {
        $("input[type='checkbox']").each(function(){
            var name = $(this).attr('name'); 
            var value = $(this).attr('value'); 

            alert(name); 
        });
     // })​;
});

作为旁注,您可以使用val()方法获取/设置复选框的值:

var value = $(this).val();