开发者

how to get the value from input control of html

I have a table which is generated by a 3rd party control. This table has only one row and one column. Within this column is html text as follows :

<p>
    this is a test</p>
<p>
    <input name="amount" type="text" value="this is for amount" /></p>
<p>
    this is a test</p>
<p>
    <input name="test" type="text" value="this is for test" /></p>
<p>
    this is a test</p>

the problem is how to get the value saved inside the html input control ? I tried the following cod开发者_Python百科e but it fails:

t.Rows[0].Cells[0].FindControl("amount");

thanks in advance...


You can use the below snippet to retrieve the required values inside the javascript function:

function getValue()
{
    //First method
    var val= document.getElementsByName("amount");
    alert("Val by Element Name:-" +val(0).value);

    //Second method
    val= document.getElementById("amount");
    alert("Val by element Id:-" +val.value);

    //third method
    val= document.getElementsByTagName("input");
    alert("Val by Tag:-" +val(0).value);
    alert("Val by Tag:-" +val(1).value);
}

Assumption: You're aware of the names of the input controls inside the table obtained by the 3rd party tool.


Do you mean when the form is posted back? In that case, just use

Request["amount"]

to get the value.


Since it is a 3rd party control, it should give you access to the values of the controls throught the properties of the controls. Check the properties and documentation.


Use a HiddenControl and jQuery.

<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
  <script>
    $(documet).ready(function(){
      var inputVals = '';
      $('#yourThirdPartyTableID').find('input[type=text]').each(function() {
         inputVals  = inputVals + $(this).val()  + ', ';
      });
      $('#<%= YourHiddenControlID.ClientID %>').val(inputVals);
    });
  </script>

The above code will insert values of all text inputs from your third party table into the hidden control separated by a comma. You can change the comma into anything else to make it easier to split on server side code.

If you're looking for the value of the input with "name" of "amount", do the following:

<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
  <script>
    $(documet).ready(function(){
      $('#<%= YourHiddenControlID.ClientID %>').val($('#yourTableID').find('[name="amount"]').val());
    });
  </script>


There is no straight forward method to retrieve the value of an input control unless you say runat="server".

My suggestion is to use the runat="server" tag and try getting the value using VALUE property.

Otherwise you can use Page.FindControl('') and see whether the control is returning other than null...then i feels that will work.

if you are using ajax or master-content page then the controls id will be not the one you given....so make sure you are giving the correct id itself (using view source of the page)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜