Javascript - 在提交表单之前删除隐藏字段或设置为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9053603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:26:44  来源:igfitidea点击:

Javascript - Remove hidden fields or set to empty before form submission

javascripthtmlformsdom

提问by user558122



I want to either remove 2 hidden fields or set them to null in a form before a POST action is performed using onclick event on a checkbox.

在使用复选框上的 onclick 事件执行 POST 操作之前,我想在表单中删除 2 个隐藏字段或将它们设置为 null。

This is what I have -

这就是我所拥有的——

<input type="hidden" id="ci" name="ci" value="2"/>
<input type="hidden" id="pg" name="pg" value="prev"/>

<form:checkboxes path="choices" items="${lookups}" itemValue="id" itemLabel="label" element="li" **onclick="this.form.submit();"**/>

How do I modify my onclick event JS to support this ?

如何修改我的 onclick 事件 JS 以支持这一点?

Thanks!

谢谢!

回答by Shadow Wizard is Ear For You

Simply change the onclickto this:

只需将其更改onclick为:

onclick="document.getElementById('ci').value = ''; document.getElementById('pg').value = ''; this.form.submit();"

You just have to use the ID of the elements then assign their value to empty string - this will still send value though. To disable any value, disable those elements:

您只需要使用元素的 ID,然后将它们的值分配给空字符串 - 这仍然会发送值。要禁用任何值,请禁用这些元素:

onclick="document.getElementById('ci').disabled = true; document.getElementById('pg').disabled = true; this.form.submit();"

回答by user558122

$("input[type='hidden']").each(function(){

$("input[type='hidden']").each(function(){

  $(this).reset();

});

});

.......................................................or......................................................

………………………………………………………………………………………………………………………………………………………… .....或者............................................ .....

The below code is made up of javascript. This may work

下面的代码由javascript组成。这可能有效

oForm = getElementById('form_id');

oForm = getElementById('form_id');

var frm_elements = oForm.elements;

var frm_elements = oForm.elements;

for (i = 0; i < frm_elements.length; i++)

for (i = 0; i < frm_elements.length; i++)

{

{

field_type = frm_elements[i].type.toLowerCase();
switch (field_type)
{
case "text":
case "password":
case "textarea":
case "hidden":
    frm_elements[i].value = "";
    break;
case "radio":
case "checkbox":
case "select-one":
case "select-multi":
default:
}

}

}

回答by tkone

Make onclick call a function which calls submit. Using jQuery is easy to do this

使 onclick 调用一个调用提交的函数。使用jQuery很容易做到这一点

function doform(){
    $.each($('input[type=hidden]'), function(input){
        $(input).val('');
    });
 }

Modify your onclick to call that. You can also do it through normal DOM access using getElementsByTagName and filtering on type or even a straight set of calls to getElementById

修改您的 onclick 以调用它。您还可以通过使用 getElementsByTagName 和过滤类型甚至直接调用 getElementById 的普通 DOM 访问来完成

function doform(){
  var i1 = document.getElementById('pg');
  i1.value = '';
  var i2 = document.getElementById('ci');
  i2.value = '';
 }

回答by Sachin jadhav

Change your onclick part like below.

如下更改您的 onclick 部分。

onclick="javascript:Clicked();"

Then define your onclick function in clientside javascript under head tags.

然后在客户端 javascript 中的 head 标签下定义您的 onclick 函数。

<Head>
<script language=javascript>
function Clicked()
{
 //1. Write code to hide fields
 document.forms[0].submit(); 
}
</script>
</Head>