Jquery:Textarea形式未序列化

时间:2013-05-30 03:40:07

标签: jquery forms serialization

基本上我想打开一个Modal确认来删除一个项目并在textarea元素中询问这样做的原因。然后,我想通过ajax将textarea的值发送到外部文件,但它的值不会通过。

脚本(edit_inventory.php)。对#retire按钮的单击进行模态确认,模态消息确认并打印已发送的内容。

<script>
  $(function() {
$("#retire").click(function() {
    var ret_data = $("#new_equipment").serialize();
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height: 500,
      modal: true,
      buttons: {
        "Remove Item": function() {
    $.ajax({
      type: "POST",
      url: "retire.php",
      data: ret_data,
    }).done(function( msg ) {
            $("#ooi").text(''+msg);
            $( "#dialog-message" ).dialog({
                  modal: true,
                  buttons: {
                    Ok: function() {
                      $( this ).dialog( "close" );
                    }
                  }
                });
    });
          $( this ).dialog( "close" );
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      }
    });
});
  });
  </script>

HTML / PHP(edit_inventory.php)

if (!empty($eq_id) && $action == "") {
echo '<form name="new_equipment" id="new_equipment" action="edit_inventory.php" method="post">';
echo '<table>';

$status = show ($eq_id);

echo '<input type="hidden" name="action" value="edit">';
echo '<tr><td colspan="4"><input type="submit" value="Save" class="button">
<a href="eq_print_label.php?id='.$eq_id.'" class="button">Print Label</a>';

if($status['eq_status']!=3) { //it is !=3
echo ' <div class="button" id="retire">RetireAAA</div>';

echo'
<div id="dialog-confirm" title="Delete this piece of equipment?" style="display: none; ">
  <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
  This item will be removed from the inventory. Are you sure?</p>
  <p>Reason:</p><textarea name="retire_reason" rows="10" cols="30" maxlength="150"></textarea>
</div>';

echo '<div id="dialog-message" title="Out of Inventory" style="display: none; ">
  <p>
    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
    <div id="ooi"></div>
  </p>
</div>';
}
echo'</td></tr>';
echo '</table></form>';
}

函数show()回显表单,其中包含从数据库获取的所有字段值。并返回一个状态(也来自数据库)!= 3(这就是为什么我看到'RetireeAAA')

我们的retiree.php

<?php
print_r ($_POST);
//just to test
?>

最后,retiree.php打印:

Array ( [eq_id] => 423A3606 ... [id] => 1111111174 [action] => edit [retire_reason] => )

Google Developer's Tool:

eq_id:423A3606
...
id:1111111174
action:edit
retire_reason:

无论我在retire_reason textarea中写什么,都没有通过。传递内容的唯一方法是在editinventory_edit.php中默认值,再次,无论我在textarea中写什么,它都会传递默认设置的值。

为什么textarea retire_reason值未被序列化或传递?

更多信息: 我在ajax调用之前将serialize()行移到了右边。在这种情况下,<div id="dialog-confirm">内没有元素通过。此外,没有任何内容从retiree.php打印回来

2 个答案:

答案 0 :(得分:0)

尝试将“retire_reason”的字段名称更改为完全不同的名称,并删除其他属性。请尝试使用标准输入。看看它是否重复了一遍又一遍发生的事情。这不是答案,但它应该有助于调试。

答案 1 :(得分:0)

我不得不改变方法并在函数中添加新表单。此外,我需要在打印表单后将序列化移动到:

$("#retire").click(function() {

var idNumber = $("#form_fields").find('.id').val();

$('#form_fields').append('<form name="retire_form" id="retire_form"><input type="hidden" name="id" value="'+idNumber+'"><input type="hidden" name="retire" value="retire"><textarea name="retire_reason" id="retire_reason" rows="10" cols="30" maxlength="150"></textarea></form>');
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height: 500,
      modal: true,
      buttons: {
        "Remove Item": function() {
    var ret_data = $("#retire_form").serialize();
    $.ajax({
      type: "POST",
      url: "retire.php",
      data: ret_data,
    }).done(function( msg ) {
            $("#ooi").text(''+msg);
            $( "#dialog-message" ).dialog({
                  modal: true,
                  buttons: {
                    Ok: function() {
                      $( this ).dialog( "close" );
                    }
                  }
                });
    });
          $( this ).dialog( "close" );
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      }
    });
});
  });

HTML:

if($status['eq_status']!=3) {
echo ' <div class="button" id="retire">RetireAAA</div>';

echo'
<div id="dialog-confirm" title="Delete this piece of equipment?" style="display: none; ">
  <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
  This item will be removed from the inventory. Are you sure?</p>
  <p>Reason:</p><div id="form_fields"><input type="hidden" name="id" class="id" value="'.$eq_id.'"></div>
</div>';

echo '<div id="dialog-message" title="Out of Inventory" style="display: none; ">
  <p>
    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
    <div id="ooi"></div>
  </p>
</div>';
}