views:

26

answers:

2

I am using the following code to post my form data to player/index.php and open it in a dialog. Because I have multiple of these forms in my table I need to use $(this).

But now it doesn't open in a dialog.

New code (doesn't open dialog but display data in url):

  $("#recordingdialog").dialog({
  //other options, width, height, etc...
     modal: true,
      bgiframe: true,
      autoOpen: false,
      height: 200,
      width: 350,
      draggable: true,
      resizeable: true,
      title: "Play Recording",});

$(this).click(function() {
  $.post('player/index.php', $(this).attr('form').serialize(), function (data) {
       $("#recordingdialog").html(data).dialog("open");
  });
  return false;
});

Old code (only works on one form):

      $("#recordingdialog").dialog({
  //other options, width, height, etc...
     modal: true,
      bgiframe: true,
      autoOpen: false,
      height: 550,
      width: 550,
      draggable: true,
      resizeable: true,
      title: "Play Recording",});


$("#wavajax button").click(function() {
  $.post('player/index.php', $("#wavajax").serialize(), function (data) {
       $("#recordingdialog").html(data).dialog("open");
  });
  return false;
});
+1  A: 

Instead of .attr() what you want is .closest(), like this:

$(this).click(function() {
  $.post('player/index.php', $(this).closest('form').serialize(), function (data) {
       $("#recordingdialog").html(data).dialog("open");
  });
  return false;
});

This gets the closest parent element that's a <form>...so the <form> you're currently in :) Also instead of $(this) as your selector for the click I think you meant any of these buttons, if that's the case give them a class like <button class="dialogBtn"> and use that selector, like this:

$(".dialogBtn").click(....);
Nick Craver
+1, if they are buttons then he could also use `$(this.form)`.
Gaby
@Gaby - Completely agree, use the DOM property is available...I wasn't sure what he's clicking on since the selector changed, might be a `<div>` or something.
Nick Craver
A: 

You should have a look at the console output for errors.

 $(this).attr('form')

will return a string and you cannot use .serialize() on a string.

Also in what context do you execute the code? Before you had an selector but now you are only using this.

Felix Kling