所以我正在尝试将 POST 输入添加到 Onclick 函数
上的表单中。因此,当我通过 Javascript 检查信息是否正常时,在通过 Javascript form.submit() 之前,我想添加更多信息,例如 IdCrono。
我现在在 Form.action = "myPhpFunction.php"
中所做的是通过操作添加 GET 变量,因此它将是
"myPhpFunction.php?key=1&IdCrono=1"
无论如何要通过 javaScript 将 Key 和 IdCrono 添加到 POST 中吗?
我的代码如下所示:
function GuardarMilestone(a) {
var id = a.id;
var idCrono = Math.floor(a.id / 10);
alert(idCrono);
var form = document.getElementById("Form" + id);
if (id % 10 == 0) {
//alert("Resto 0");
var date = form.elements["FechaInicio"];
} else {
//alert("Resto 5");
var date = form.elements["FechaFin"];
}
if (date.value != "") {
form.action = "InteriorInternalFunciones.php?key=1&id=" + id;
form.target = "_self";
form.submit();
} else {
alert("Please fill in the date before saving");
}
}
<form method="POST" id="Form<?php echo $rstMilestones->fields['IdCronograma']; ?>0" action="">
<input type="date" style="font-size: 9px; font-style: bold;" name="FechaInicio" value="<?php echo $strFaseInicioSinFormato;?>">
<input type="hidden" name="IdCronograma" value="<?php echo $rstMilestones->fields['IdCronograma']; ?>">
<a href="#botones" id="<?php echo $rstMilestones->fields['IdCronograma']; ?>0" onclick="GuardarMilestone(this)"><img src="images/PC_si.png" alt="Save"></a>
<a href="#botones" id="<?php echo $rstMilestones->fields['IdCronograma']; ?>0" onclick="EditarFechaCronograma(this)"><img src="images/PC_no.png" alt="cancel"></a>
</form>
最佳答案
也许这会有所帮助,只需将此函数添加到您的代码中即可:
function addFields(form, inputName, inputValue) {
// Create an <input> element
var input = document.createElement("input");
input.type = "hidden";
input.name = inputName;
input.value = inputValue;
form.appendChild(input);
}
并替换这一行:
form.action = "InteriorInternalFunciones.php?key=1&id=" + id;
这样:
form.action = "InteriorInternalFunciones.php;
addFields(form, "key", 1);
addFields(form, "id", id);
这将在提交表单之前向您的代码添加隐藏字段,并且所有字段都将通过 POST 方法发送。
关于javascript - 如何通过javascript向表单添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43586023/