简体   繁体   English

如何从JavaScript更新隐藏字段值

[英]How to update a hidden field value from JavaScript

I have a JSP page where I Select 5 item rows. 我有一个JSP页面,我在其中选择5项目行。 I am adding the Select Id s in sdata array. 我在sdata数组中添加Select Id I added a hidden button. 我添加了一个隐藏的按钮。 alert is showing the full list. alert显示完整列表。 How can I set this list to the hidden field's( mids ) value? 如何将此列表设置为隐藏字段的( mids )值?

<input type="hidden" name="mids" value="">

JavaScript: JavaScript:

$( document ).ready(function() {

var sdata =new Array();

  fn-create-ids{
     (checckbox.each.check())
      {
        sdata.push(i);
      }
      document.GetelementbyName("mids").value=sdata;
      alert(document.GetelementbyName("mids").value);---shows value
   }
 If I add it here, it is empty
 document.GetelementbyName("mids").value=sdata;---empty
} 

Give these hidden fields some ids 给这些隐藏的字段一些ID

and then use 然后使用

document.getElementById('hiddenfieldID').value +="your value";

in javascript 在JavaScript中

Example : 范例

  document.getElementById('mid').value += sdata;

BUT

Really didn't understand how this function works 真的不明白这个功能是如何工作的

fn-create-ids{
     (checckbox.each.check())
      {
        sdata.push(i);
      }
   }

i think the value should be i 我认为价值应该是i

document.getElementById('mid').value +=i;

Sajad's answer is valid, but if you want to keep using just names, here's how. Sajad的答案是正确的,但是如果您想继续使用名称,请按以下步骤操作。 (You may already have a form in your document; if so, then just use the existing 'name' attribute for the Javascript) (您的文档中可能已经有一个表格;如果是这样,则只需对Javascript使用现有的“名称”属性)

<form name="midsForm">
  <input type="hidden" name="mids" value="">
</form>

Then: 然后:

document.forms.midsForm.mids.value = "your value";

I have a header column which is a Checkbox(parent-check). 我有一个标题栏,它是一个Checkbox(parent-check)。 Dynamically, I am filling bottom rows. 动态地,我正在填充底部行。 Now when I select "parent-check", it is selecting all 13 rows that come from db. 现在,当我选择“ parent-check”时,它将选择来自db的所有13行。 I have a submit button at the bottom outside of my display tag. 我在展示广告代码底部的底部有一个提交按钮。 when I click submit, I am going to another page. 当我单击提交时,我将转到另一个页面。 I will Select the check box(parent-check) to select all rows and then I will click submit. 我将选中复选框(父复选框)以选择所有行,然后单击提交。 By this time, I want javascript to update my hidden field value with the array that will contain all selected row ids. 到这个时候,我希望javascript用包含所有选定行ID的数组更新我的隐藏字段值。 Jsp: In my existing script, I found this: Jsp:在我现有的脚本中,我发现了这一点:

$(document).ready(function() {

var sdata =new Array();

  $("#parent-check").click(function() {
     if ($(this).prop("checked")) {
        $("td>:checkbox").each(function() {
            sdata.push($(this).attr("mids");
        });
      }
      document.GetelementbyName("mids").value=sdata;
      alert(document.GetelementbyName("mids").value);---shows value
   }
 If I add it here, it is empty
 document.GetelementbyName("mids").value=sdata;---empty
} 

It is getting the array list inside a function. 它正在函数中获取数组列表。 But the list becomes empty when I come out of the function though I declare it outside of the function under $( document ).ready(function(). 但是,尽管我在$(document).ready(function()下的函数外部声明了该列表,但是当我离开该函数时,该列表却为空。

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

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