简体   繁体   English

将 td 元素的 HTML 设置为隐藏字段的值

[英]Set HTML of td element as value of hidden field

I wanted to know how would I go about setting the HTML of my td element as the value of a hidden field.我想知道如何将 td 元素的 HTML 设置为隐藏字段的值。

<td align="center">
       <%if (inst_dm != null) {%>

    ...some code..
</td>
  <%} else {%>

<td align="center"> Contact not available.
   <%}%>
  <input type="hidden" name="inst_dmhidden" value="<%$(this).html().trim(); %>">

</td>

So, what I basically want is, in the input field inst_dmhidden, either the value from (..some code..) part or 'Contact not available'.所以,我基本上想要的是,在输入字段 inst_dmhidden 中,来自(..some code..)部分的值或“联系方式不可用”。

Any thoughts about how to go about doing this?关于如何去做这件事的任何想法?

-Pritish. -英国人。

Give your td and hidden element an id or a way to easily locate as I've done here为您的 td 和隐藏元素提供一个 id 或一种轻松定位的方法,就像我在这里所做的那样

<td align="center" id="mytd">
     <%if (inst_dm != null) {%>

     ...some code..
</td>
  <%} else {%>

<td align="center" id="mytd"> Contact not available.
   <%}%>
  <input type="hidden" id="myhiddenfield" name="inst_dmhidden" value="<%$(this).html().trim(); %>">

</td>

Then using jQuery you could run this code:然后使用 jQuery 您可以运行此代码:

$("#mytd").html($("#myhiddenfield").val());

UPDATE更新

In the case where you don't want to use IDs you could run some variant of this code :在您不想使用 ID 的情况下,您可以运行此代码的某些变体:

$("td").each(function(index) {
    var td = $(this);
    td.html(td.find("input[type=hidden]").val());
});

The above code is assuming the hidden field is inside the td, but you can change that accordingly.上面的代码假设隐藏字段在 td 内,但您可以相应地更改它。

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

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