[英]How can I get the value of a control whose ID was passed to javascript function from code behind
I'm not sure how to handle the issue to get a value of the control if its ID was passed to a function from code behind. 如果将控件的ID从后面的代码传递给函数,我不确定如何处理该问题以获取控件的值。
I have a function called StateSelect that is added to a control in code behind. 我有一个称为StateSelect的函数,该函数在后面的代码中添加到控件中。 It has parameters.
它具有参数。 The last parameter is
txtCity.ClientID.ToString()
最后一个参数是
txtCity.ClientID.ToString()
In my html, I have this function defined as 在我的html中,我将此函数定义为
function StateSelectedp(city, state, ddlZipName, txtCityID)
{
alert($.trim($('#txtCityID').value)); //not sure how to do it
}
I need to get a value of the txtCityID control. 我需要获取txtCityID控件的值。 How can I reference it with jQuery?
如何使用jQuery引用它?
Thank you 谢谢
To get the value of an element with a variable ID, you can do something along the lines of the following... 要获取具有可变ID的元素的值,可以按照以下步骤进行操作...
function StateSelectedp(city, state, ddlZipName, txtCityID)
{
alert($('#' + txtCityID).val());
}
txtCityID is a string representing the whole ID of a given element. txtCityID是一个字符串,代表给定元素的整个ID。 For jQuery to search by id, the id must be prepended by the '#' character.
为了让jQuery通过id搜索,id必须以'#'字符开头。 After the element has been selected, the val() function will return the value of the first (I think it's only first, anyways) element in the selected set of elements.
选中元素后,val()函数将返回所选元素集中的第一个(无论如何,我认为它只是第一个)元素的值。 There should only be one anyways, as only one element should have a given id.
无论如何都应该只有一个,因为只有一个元素应该具有给定的ID。
Keep in mind, though, that this is all happening on the client side. 但是请记住,这都是在客户端发生的。 Your reference to code-behind (a Web Forms term) implies that you might be intending to do something with this on the server-side, so this may or may not be the path you're really looking for.
您对代码隐藏(Web窗体术语)的引用意味着您可能打算在服务器端对此进行操作,因此这可能不是您真正想要的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.