简体   繁体   English

通过JavaScript提交之前更改表单值

[英]Change a form value before submitting via JavaScript

I've been through a lot of questions on similar issues, but can't seem to get this working. 我在类似问题上遇到了很多问题,但似乎无法解决这个问题。 What I'm trying to do is when a button is clicked, replace the value of a form field with the word 'now' and then submit the form. 我想做的是单击一个按钮时,将表单字段的值替换为单词“现在”,然后提交表单。 Initially, the HTML form looks like this: 最初,HTML表单如下所示:

<tr id="row43" bgcolor="#FF0000">
<form id="form43" name="loggingUpdate" action="index.php" method="POST">
<td>PersonsName</td><td>SGS 1-26</td>
<td><input type="text" name="takeoff" id="takeoff43"/><a href='#' onclick='startTakeoff(43);return false;'><img alt='Start Now' src='brush_24.png' border='0'></a></td>
<td><input type="text" name="landing" /></td><td>0 Mins</td>
<td><input type="text" name="towHeight" /></td>
<td><input type="hidden" name="flightIndex" value="43"/><input type="submit" value="Update..." />  <a href=deleteEntry.php?flightIndex=43><img src="close_24.png" /></a></td>
</form></tr>

The Javascript that's triggered by the onclick is as shown below: 由onclick触发的Javascript如下所示:

function startTakeoff(flightIndex) { 
    var flightForm = document.getElementById("form"+flightIndex);
    var flightRow = document.getElementById("row"+flightIndex);

    flightRow.cells[2].innerHTML = "<input type=\"text\" name=\"takeoff\" value=\"now\"><a href=\"#\" onclick=\"startTakeoff(43);return false;\"><img alt=\"Start Now\" src=\"brush_24.png\" border=\"0\"></a>";
    flightForm.submit();
}

So when the button is clicked, it should replace the cell's value with 'now' and submit the form. 因此,单击按钮时,应将单元格的值替换为“ now”并提交表单。 The trouble is while it does submit, the changed value doesn't actually get submitted. 问题是它确实提交了,但是更改后的值实际上没有提交。 If I manually add something to one of the other fields (the towHeight for example) and click the button, it does get submitted successfully. 如果我手动向其他字段之一添加内容(例如towHeight),然后单击按钮,则确实提交成功。 In other words, it is only the 'takeoff' field that doesn't get submitted when the button is clicked. 换句话说,单击按钮时,只有“起飞”字段不会提交。 I've never worked with Javascript before, so this seems very odd to me. 我以前从未使用过Javascript,因此这对我来说似乎很奇怪。

In the way of background, there is a PHP script that's generating the HTML and taking the submitted input, hence some of the strange IDs. 在背景中,有一个PHP脚本生成HTML并接受提交的输入,因此还有一些奇怪的ID。 The entire page can be seen here in case I've left anything out http://ad7zj.net/logging/index.php I'd appreciate the help! 如果我遗漏了任何内容,则可以在此处看到整个页面。http://ad7zj.net/logging/index.php我非常感谢您的帮助!

You are looking for the .onsubmit() event. 您正在寻找.onsubmit()事件。

That said, you might be best off just getting the time from the server. 也就是说,最好只是从服务器上获取时间。 It will save you from a whole world of time zones, day light savings time issues, and the number of computers out there with inaccurate time settings. 它将为您节省整个时区,日光节约时间问题以及时间设置不正确的计算机数量。

EDIT: 编辑:

Ok, thanks to your comment I think I see what is going on. 好的,感谢您的评论,我想我知道发生了什么事。

You are trying to update the form using .innerHTML . 您正在尝试使用.innerHTML更新表单。 .innerHTML is a rather strange beast and full of all sorts of heartache -- yield not to its temptations. .innerHTML是一种非常奇怪的野兽,充满了各种各样的心痛-屈服于它的诱惑。 It looks like the browser is waiting till the Javascript event is over before actually applying the innerHTML update. 看起来浏览器正在等待Javascript事件结束,然后才实际应用innerHTML更新。 That would be my guess, anyways. 无论如何,那是我的猜测。 Outside of the simplest of updates, I avoid the thing. 除了最简单的更新之外,我避免这种情况。

Nuke this: 核销此:

  flightRow.cells[2].innerHTML = "<input type=\"text\" name=\"takeoff\" value=\"now\"><a href=\"#\" onclick=\"startTakeoff(43);return false;\"><img alt=\"Start Now\" src=\"brush_24.png\" border=\"0\"></a>";

Try this instead: 尝试以下方法:

// Assuming only one form with one name 'takeoff'
var el = document.getElementsByName("takeoff")[0];
el.value = 'now';

// The rest of the updates shouldn't matter -- the form is being submitted, nothing should be seen.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM