简体   繁体   English

如何从后面的代码中获取ID传递给javascript函数的控件的值

[英]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.

相关问题 我怎样才能从ascx代码后面获取值ID - How can I get value ID from ascx code behind 如何从 javascript function 中传递的控制 ID 中获取检查属性的值? - How to get checked property's value from passed control id in javascript function? 如何获取ID动态变化的HTML控件的值? (在JavaScript中) - How to get the value of an HTML control whose ID changes dynamically? (in JavaScript) 如何在JavaScript函数中使用/引用所选值(TextBox)到后面的C​​#代码中? - How can I use/reference the selected value (TextBox) from a JavaScript Function into C# Code behind? 从后面的代码将控制 ID 传递给 javascript - Passing control id to javascript from code behind 如果ImageButton是在后面的代码中创建的,如何获取javascript的ImageButton的ID? - How can I get the ID of ImageButton for javascript, if the ImageButton was created in code behind? 如何将控件值从父窗口传递到后面的弹出窗口代码 - how can I pass control value from parent window to popup window's code behind 如何将值从代码背后传递给WP7中的javascript函数 - How to pass a value from code behind to javascript function in WP7 在代码后面执行 JavaScript function — 如何在代码后面获取返回值 - Executing JavaScript function in Code behind — How to Get the Return Value in Code Behind 将代码背后的值传递给JavaScript函数 - Pass Value from Code Behind to JavaScript Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM