-1

我尝试设置复选框并且无法获得工作简单的代码。

有什么线索吗?

谢谢!

@{
    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>
4

1 回答 1

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(); 
于 2012-08-14T14:03:06.123 回答