无法使用ajax获取按钮的值

时间:2015-07-17 11:46:57

标签: php jquery ajax codeigniter

以下是我的代码。我想在AJAX部分中获取提交按钮的值,但我无法做到这一点。请告诉我如何获取按钮的值。

<script src="http://localhost/ci/js/jquery.js"></script>
<script type="text/javascript">
    // Ajax post
    $(document).ready(function() {
        $('#f input').on('change', function() {
            //alert("hio");
            var b = $('input[name="r5"]:checked', '#f').val();
            //alert(b);
            $("#h1").val(b);
        });

        $(".submit").click(function(event) {
            event.preventDefault();
            var ans = $("input#h1").val();
            var que = $("input#h").val();
            var no = $("input#h2").val();
            var de = $("input.submit").val();

            jQuery.ajax({
                type: "POST",
                url: "http://localhost/ci/index.php/controller_q/quchng",
                dataType: 'json',
                data: {
                    ans: ans,    
                    que: que,
                    no: no,
                    de: de            
                },
                success: function(res) {
                    jQuery('#i').html(res);
                }
            });
        });
    });
</script>

这是我的观看页面代码:

<div id="i">
    <?php $a = 1; ?>
    Time left- <span id="countdown" class="timer"></span>
    Q: <?php echo $qno['ques'.$a];?>

    <form id="f">
        <input type="radio" name="r5" id="r1" value="<?php echo $qno['a'.$a];?>"><?php echo $qno['a'.$a];?><br>
        <input type="radio" name="r5" id="r2" value="<?php echo $qno['b'.$a];?>"><?php echo $qno['b'.$a];?><br>
        <input type="radio" name="r5" id="r3" value="<?php echo $qno['c'.$a];?>"><?php echo $qno['c'.$a];?><br>
        <input type="radio" name="r5" id="r4" value="<?php echo $qno['d'.$a];?>"><?php echo $qno['d'.$a];?><br>

        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="b" class="submit" value='po'>Save & Next</button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button id="b" class="submit" value='po'>Save  Next</button>

        <input type="hidden" id="h" value="<?php print_r($qno);?>">
        <input type="hidden" id="h1">
        <input type="hidden" id="h2" value="<?php echo $a;?>">
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

这是因为你做错了: -

 $(".submit").click(function(event) {
      var de = $(this).val(); // check which button is clicked and get that button value
      alert(de);return false; // this is just to check value is coming or not.

检查一下,你就会得到你的价值。谢谢。