1

I have a litle problem with the following code:

<script type = "text/javascript" >
$(document).ready(function() {
    $("#click").click(function() {
        var jegy = $(this).attr('value');
        var dataString = $("#jegyform").serialize();

        $.ajax({
            type: "POST",
            url: "jegybeiras.php",
            data: dataString + '&jegy=' + jegy,
            cache: false,
            success: function(html) {
                $("#jegyek").html(html);
            }
        });

    });
}); 
</script>

And a form where is the following buttons (and other inputs)

<button id="click" name="jegy" type="submit" value="p5">5</button>
<button id="click" name="jegy" type="submit" value="p4">4</button>
<button id="click" name="jegy" type="submit" value="p3">3</button>
<button id="click" name="jegy" type="submit" value="p2">2</button>
<button id="click" name="jegy" type="submit" value="p1">1</button>

My question is that, how can I post the clicked button value, it is working now but randomly for 4th-5th clicking on the actual button.

0

1 Answer 1

2

Convert id to class= "click" in each your button input and then try your code.

<script type="text/javascript">
 $(document).ready(function() {
    $(".click").click(function() {
        var jegy = $(this).attr('value');
        var dataString = $("#jegyform").serialize();

        $.ajax({
            type: "POST",
            url: "jegybeiras.php",
            data: dataString + '&jegy=' + jegy,
            cache: false,
            success: function(html) {
                $("#jegyek").html(html);
            }
        });

    });
 }); 
</script>

Note:- if you want to add event on same type of element coming multiple time in your code. then go for class. id is used to add event on unique element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.