最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Unable to get value Textbox by id in javascript - Stack Overflow

programmeradmin1浏览0评论

I have text boxes and it has values let say. I am calling my javascript method on one of textbox 'onblur'. my function is:

function CalculateExpectedProductPrice() {
  alert("hi i called");
  var originalPrice = document.getElementById('hdnSelectedProductPrice').value;

  var numberOfLicenses = document.getElementById('txtNumberOfLicense').value;
  var enteredAmount = document.getElementById('txtAmount').value;

  alert(originalPrice);
  alert(numberOfLicenses);
  alert(enteredAmount);

}

i am getting alert message as ""hi i called". but not further.

But some i am not getting values of these controls.

*Edited:* My HTML is :

<asp:HiddenField ID="hdnSelectedProductPrice" runat="server" />
<asp:TextBox ID="txtAmount" runat="server" Width="250px"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:TextBox ID="txtNumberOfLicense" runat="server" Width="35px" ></asp:TextBox>
   </ContentTemplate>
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="txtNumberOfLicense" EventName="" />
   </Triggers>
</asp:UpdatePanel>

Will there any impact master-content page . because script is in content page and html also on same content page.Also let me know you, I am using wizard control where as all these controls are resides on second step of wizard. will that make any impact ?

Edited:

I think wizard control making here matter. As i started my firebug and review the generated html it assign the Id dynamically to those controls which are inside the wizard. thats why javascript unable to find the expected control . eg for txtAmount text box which is inside the wizard control getting name as :

ctl00_ContentPlaceHolder1_Wizard1_txtAmount

but certainly i would not prefer to use this generated Id. So is there any remedy to find control inside the wizard control and get - set values ?

I have text boxes and it has values let say. I am calling my javascript method on one of textbox 'onblur'. my function is:

function CalculateExpectedProductPrice() {
  alert("hi i called");
  var originalPrice = document.getElementById('hdnSelectedProductPrice').value;

  var numberOfLicenses = document.getElementById('txtNumberOfLicense').value;
  var enteredAmount = document.getElementById('txtAmount').value;

  alert(originalPrice);
  alert(numberOfLicenses);
  alert(enteredAmount);

}

i am getting alert message as ""hi i called". but not further.

But some i am not getting values of these controls.

*Edited:* My HTML is :

<asp:HiddenField ID="hdnSelectedProductPrice" runat="server" />
<asp:TextBox ID="txtAmount" runat="server" Width="250px"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:TextBox ID="txtNumberOfLicense" runat="server" Width="35px" ></asp:TextBox>
   </ContentTemplate>
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="txtNumberOfLicense" EventName="" />
   </Triggers>
</asp:UpdatePanel>

Will there any impact master-content page . because script is in content page and html also on same content page.Also let me know you, I am using wizard control where as all these controls are resides on second step of wizard. will that make any impact ?

Edited:

I think wizard control making here matter. As i started my firebug and review the generated html it assign the Id dynamically to those controls which are inside the wizard. thats why javascript unable to find the expected control . eg for txtAmount text box which is inside the wizard control getting name as :

ctl00_ContentPlaceHolder1_Wizard1_txtAmount

but certainly i would not prefer to use this generated Id. So is there any remedy to find control inside the wizard control and get - set values ?

Share Improve this question edited Mar 19, 2013 at 15:41 Mathew Thompson 56.4k15 gold badges129 silver badges150 bronze badges asked Mar 9, 2011 at 13:45 Red SwanRed Swan 15.6k44 gold badges161 silver badges241 bronze badges 2
  • yes please review the question again. I have added my HTML. and may i ask why -1 here ? since all things are right though unable to get value ...so what is my fault ? – Red Swan Commented Mar 10, 2011 at 5:26
  • i did before this so many times. but now some how i am unable to find any mistake here. can you guys please help me to sort out this? – Red Swan Commented Mar 10, 2011 at 5:44
Add a ment  | 

5 Answers 5

Reset to default 4

get id of the control as shown below

var enteredAmount = document.getElementById('<%=txtAmount.ClientId%>').value;

It's impossible to say for certain with your not having quoted your HTML (!), but the usual reason for this is confusion between the id and name attributes. document.getElementById works with the id attribute, but people tend to think it works with the name on input fields, which it doesn't (except for on IE, where getElementById is broken).

(The other thing to remember is that id values must be unique on the entire page, but looking at the IDs you quoted, I suspect you're okay on that front.)

Update: It works if you use ids:

HTML:

<form>
  <input type='hidden' id='hdnSelectedProductPrice' value='10'>
  <input type='text' id='txtNumberOfLicense' value='2'>
  <input type='text' id='txtAmount' value='3'>
  <br><input type='button' id='theButton' value='Click Me'>
</form>

Live copy

As T.J. mentioned we really need to see your html code, without seeing it it could be that you are looking for an elements attributes.

So lookup the element as you are already with

var element = document.getElementById('product');

Once you have the element you can query its attributes

var price = element.getAttribute('price');

If its "ASP server control" then you will have to do it like this:

var originalPrice = document.getElementById('<%=hdnSelectedProductPrice.ClientID %>').value;

if you use Asp.Net 4.0 and your textbox is unique on the entirepage you can add ClientIDMode="Static" in attribute of your textbox

<asp:TextBox ID="txtAmount" runat="server" Width="250px" ClientIDMode="Static"></asp:TextBox>
发布评论

评论列表(0)

  1. 暂无评论