doutang1946 2014-05-22 05:11
浏览 68
已采纳

jquery - 动态创建的表单只传递表单提交上的一些输入发布数据(表单过早关闭)

I have an entire form which is created dynamically via jQuery. It submits fine with an onclick event from a <input type="button"> at the end of the form (side note: the form does not submit with an <input type="submit">. I don't know why but it could be relevant.). My problem is that for some reason, when I print_r($_POST); after submission, only two pieces of $_POST data end up passed, namely name=selectedID and name=CustomID. The rest of the <input>s don't pass their data.

Here is how the form gets created:

var sid = servHTMLid.replace("serviceSel", "");

var cidText = $("#" + servHTMLid + " td:nth-child(2)").html();
$("#" + servHTMLid + " td:nth-child(2)").html("<form name='serviceForm' id='serviceForm' method='POST' action='services.php?save'><input type='hidden' name='selectedID' id='selectedID' value='" + sid + "' /><input type='text' name='CustomID' id='CustomID' value='" + cidText + "' />");

// seems like from here out, these inputs don't POST
var catText = $("#" + servHTMLid + " td:nth-child(3)").html();
$("#" + servHTMLid + " td:nth-child(3)").html("<input type='text' name='Category' id='Category' value='" + catText + "' />");

var nameText = $("#" + servHTMLid + " td:nth-child(4)").html();
$("#" + servHTMLid + " td:nth-child(4)").html("<input type='text' name='Name' id='Name' value='" + nameText + "' />");

var durationText = $("#" + servHTMLid + " td:nth-child(5)").html();
$("#" + servHTMLid + " td:nth-child(5)").html("<input type='text' name='Duration' id='Duration' value='" + durationText + "' />");

var priceText = parseFloat($("#" + servHTMLid + " td:nth-child(6)").html()).toFixed(2);
$("#" + servHTMLid + " td:nth-child(6)").html("<input type='text' name='Price' id='Price' value='" + priceText + "' />");

var taxText = $("#" + servHTMLid + " td:nth-child(7)").html();
if (taxText == "yes") {
  $("#" + servHTMLid + " td:nth-child(7)").html("<input type='checkbox' name='Tax' id='Tax' checked />");
} else {
  $("#" + servHTMLid + " td:nth-child(7)").html("<input type='checkbox' name='Tax' id='Tax' />");
}

var InactiveText = $("#" + servHTMLid + " td:nth-child(8)").html();
if (InactiveText == "yes") {
  $("#" + servHTMLid + " td:nth-child(8)").html("<input type='checkbox' name='Inactive' id='Inactive' checked />");
} else {
  $("#" + servHTMLid + " td:nth-child(8)").html("<input type='checkbox' name='Inactive' id='Inactive' />");
}

$("#" + servHTMLid + " td:nth-child(9)").html('<input type="button" value="save" name="serviceFormSubmit" id="serviceFormSubmit" onclick="submitForm();" /></form>');

Here is a screenshot about to submit the form (with the 'save' button) after it being dynamically created: enter image description here

Here is the print_r($_POST); output after submitting: enter image description here

Notice there is no $_POST['Category'] or $_POST['Name'] etc.

For good measure, this is the submitting function called by the button's onclick event:

function submitForm()
{
 $("#serviceForm").submit();
}

EDIT: Just found out that the generated HTML apparently is closing the form prematurely..??

   <table class="services" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr id="serviceSel6" onclick="openEdits(this.id);">
<td>6</td>
<td>
<form id="serviceForm" action="services.php?save" method="POST" name="serviceForm">
<input id="selectedID" type="hidden" value="6" name="selectedID">
<input id="CustomID" type="text" value="Massage 09" name="CustomID">
</form> // <----- THIS... why?
</td>
<td>
<input id="Category" type="text" value="" name="Category">
</td>
<td>
<input id="Name" type="text" value="Mass Reflexology" name="Name"> // .....etc.
  • 写回答

2条回答 默认 最新

  • dongyirong3564 2014-05-22 05:42
    关注

    Be careful about using the jQuery methods creating dynamic html such as html(), append() and the like.

    Any opening tag without a closing tag is given a corresponding closing tag. That's why </form> appears where you see it.

    .html("<form name='serviceForm' id='serviceForm' method='POST' action='services.php?save'><input type='hidden' name='selectedID' id='selectedID' value='" + sid + "' /><input type='text' name='CustomID' id='CustomID' value='" + cidText + "' />");
    

    html() did not see a </form> at the end of the string so it added one for you. Usually you would take your time to construct the whole form string and then you would invoke the appropriate method:

    E.g. .html( 'Well-formed HTML String' )

    JS FIDDLE <---- Check the console output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法