skip to Main Content

I was wondering how to make an ajax request separately inside the looping form, which able to send the async delete request when I click the specified row deleted button.
not sure how to allocate the identity when I click the button using jquery.

foreach(var item in Model){
  <form>
    <input type="text" id="id" name="id" value="item.id"/>
    <input type="button" id="btn" name="submit" value="Delete"/>
  </form>
}

<script>
  $("#btn").click(function(){
          // alert the id value
      });
</script>

2

Answers


  1. Chosen as BEST ANSWER

    I have found the solution based on Agus Friasana method:

    $(".btn").click(function () {
            // to skip the alert from parent modal button
            if ($(this).attr("data-id") != undefined) {
                alert($(this).attr("data-id").toString());
                $.ajax(
                    {
                        url: "your url" + $(this).attr("data-id").toString(),
                        type: "DELETE",
                        dataType: "text",
                        success: function () {
                            alert('success');
                        },
                        error: function () {
                            alert('fail')
                        }
                    }
                );
            }
        });
    

  2. You can try with

    foreach(var item in Model){
      <form>
        <input type="text" id="id" name="id" value="item.id"/>
        <input type="button" class="btn" data-id="item.id" name="submit" value="Delete"/>
      </form>
    }
    
    <script>
      $(".btn").click(function(){
        // alert the id value
        alert($(this).attr("data-id"))
      });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search