更改ID并使用ajax

时间:2016-02-05 20:41:55

标签: javascript jquery html ajax

我正在尝试更改此按钮的ID,然后使用ajax,但不起作用。 任何人都可以帮助我吗?

我的HTML代码:

<form action="" method="POST" class="button">
    <input type="hidden" id="userid" value="<?=$_GET["userid"]?>" />
    <button id="button" class="btn btn-primary"> Wait </button>
</form>

我的jQuery代码:

<script type="text/javascript">
jQuery(document).ready(function ($) {
    $('#button').countTo({
        interval: 1000, startNumber: 2, endNumber: 0, onLoop: function (self, current, loop) {
            $(self).text('Wait: ' + current);
         },
        onFinish: function (self, current, loop) {
            document.getElementById("button").innerHTML = "close";
            document.getElementById("button").id = "go";
        }
    });
});
</script>

我的Ajax代码:

$(document).ready(function() {
    $('#go').click(function() {
        var userid    = $('#userid').val();
        var dataString = 'userid=' + userid;
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: dataString,
            dataType: "json",
            success: function(data) {
                var success = data['success'];
                if (success == false) {
                    console.log('false');
                }
                if (success == true) {
                	console.log('true');
                }
            },
            error: function(data) {
                console.log(data);
            }
        }); 
        return false;
    });
});

我认为这是我改变id的原因,因为如果我使用第一个id(按钮),就会有效

1 个答案:

答案 0 :(得分:1)

尝试

 $(document).on("click","#go",function(){})