如何在Master Pages中使用javascript获取隐藏字段值 [英] How to get hidden field value using javascript in Master Pages

查看:73
本文介绍了如何在Master Pages中使用javascript获取隐藏字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

谁能告诉我,我怎样才能使用javascript获取隐藏字段的价值。我正在使用母版页。



这是aspx代码

Hello Friends,
Can anyone tell me, how I can get the value of hidden field using javascript. I''m using master pages.

Here is aspx code

<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CssClass="delbutton" CommandArgument=''<%#Eval("noticeid") %>''>Delete
<asp:HiddenField ID="hfID" Value=''<%# Eval("noticeid") %>'' runat="server" />





在这里,我正在尝试,我对隐藏的领域具有约束力。使用这个值我将使用Json删除记录。



我试过以下JavaScript



Here what I''m trying, I''m binding value to hidden field. Using this value I''m going to delete record using Json.

I tried following JavaScript

var id = document.form1.item("hfID").value;
alert(id);





它给出错误说:document.form1未定义

这个错误是由于使用母版页,在母版页中,表单有 id =''form1''



任何想法,如何获得价值?



提前谢谢



It gives error saying: document.form1 is undefined
Is this error due to use of master page, in master page the form has id=''form1''

Any idea, how to get value??

Thanks in advance

推荐答案

获得解决方案



通过查看页面源来检查hiddenfield的ID。



Got solution

check the id of hiddenfield by viewing page source.

document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;





这是我给隐藏归档的实际ID: hfID



这是由asp.net添加的: ctl00_cphMain_gvJobs_ctl05 _



所以如果你试图使用



This is what actual id I have given to hidden filed:hfID

This is added by asp.net:ctl00_cphMain_gvJobs_ctl05_

So if you try to use

document.getElementById("hfID").value;





它给你错误说: document.getElementById(hfID)。value is null



所以,最终解决方案是



it gives u error saying: document.getElementById("hfID").value is null

So, final solution is

document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;


虽然您给出的答案是正确的,更好的方法是使用ClientID为代码中的隐藏控件发出值,如果通过查看源找到值,则该值可以更改。使用ClientID永远不会中断。
While the answer you''ve been given is ''correct'', a better way is to use the ClientID for your hidden control in code to emit the value, if you find the value by looking at your source, that value can change. Using ClientID will never break.


像CG一样说使用ClientID。

Like CG said use the ClientID.
var id = document.getElementById(''<%= hfID.ClientID %>'').value;
alert(id);





进一步阅读

控制。 ClientID属性 [ ^ ]

如何在没有丑陋的JavaScript中使用ClientIDs [ ^ ]


这篇关于如何在Master Pages中使用javascript获取隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆