weixin_33688840 2016-10-22 13:53 采纳率: 0%
浏览 43

jQuery只返回一个值

This is my first stackoverflow question. I am working on simple ajax code. The idea is that i have multiple checkboxes. When you click on checkbox, the jquery script needs to take the value of checkbox which is pressed and send ajax request.

The problem is that whatever checkbox i pressed the value is 321 which is the first checkbox value. I am new to javascript so sorry if question is stupid or something. Thank you.

        </title>
    </head>
    <body>

    <form action="">
    <input id="321" class="checkbox" type="checkbox" name="vehicle" value="321" />I have a car<br>
      <input id="123" class="checkbox" type="checkbox" name="vehicle" value="123" /> I have a bike<br>

      <input type="submit" value="Submit"/>
    </form>

    <span></span>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var sum = 0;
        $(".checkbox").change(function(){
            if($(this).is(':checked')) {
                var str = $(".checkbox").attr("value");
                $.get("demo.php?item="+str, function(data, status){
                    sum = sum + parseInt(data);
                    $("span").html(sum);
                });
            }else{
            var str = $(".checkbox").attr("value");
            $.get("demo.php?item="+str, function(data, status){
                sum = sum - parseInt(data);
                $("span").html(sum);
            });
        }
    });

});

</body>
</html>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题