使用javascript更改后获取文本框值 [英] Get textbox value after changing with javascript

查看:71
本文介绍了使用javascript更改后获取文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下拉列表中的javascript更改文本框的值更改(无回发)。文本框正确更新,但提交表单时未提供值。我认为这是因为javascript更改只发生在客户端?如何在服务器端获得此值?



我尝试过:



< pre> < script type =text / javascript> 
function ddlChange(){
var ddl = document.getElementById('<%= DDLg4corp.ClientID%>');
var textBox = document.getElementById('<%= txtg4action.ClientID%>');

textBox.value = ddl.options [ddl.selectedIndex] .value;
}
< / script>

< asp:DropDownList runat =serverID =DDLg4corponchange =ddlChange();>
< asp:ListItem Value =1Text =Option 1>< / asp:ListItem>
< asp:ListItem Value =2Text =Option 2>< / asp:ListItem>
< asp:ListItem Value =3Text =Option 3>< / asp:ListItem>
< asp:ListItem Value =4Text =Option 4>< / asp:ListItem>
< / asp:DropDownList>

< asp:TextBox runat =serverid =txtg4action>< / asp:TextBox>





 Private Sub Button_Click(发件人为对象,e为EventArgs)处理btnSubmit1.Click,


更新
.Connection = connection
.CommandType = CommandType.Text
如果qrtSelect.SelectedValue = 1则
.CommandText =更新表集txtg4action = @ txtg4action其中users = @ users
.Parameters.AddWithValue(@ txtg4action,txtg4action.Text)

解决方案

< blockquote>嗯,正如所承诺的,这是一个使用你的代码的简单页面:

 <%@  < span class =code-attribute>   Page    语言  =  VB   %>  
< script runat = server >
私有 Sub btnGo_Click( ByVal 发​​件人 As 对象 ByVal e As EventArgs )
litX.Text = 您选择了& txtg4action.Text
结束 Sub
< / script >
< html >
< head >
< title > < / title >
< script type = text / javascript >
function ddlChange(){
var ddl = document .getElementById(' <%= DDList.ClientID%>');
var textBox = document .getElementById(' <%= txtg4action.ClientID%>');
textBox.value = ddl.options [ddl.selectedIndex] .value;
}
< / 脚本 >
< / head >
< body >
< 表单 id = Form1 runat < span class =code-keyword> = server >
< p > < asp:DropDownList < span class =code-attribute> runat = server ID = DDList onchange = ddlChange(); >
< asp:ListItem Value = 1 < span class =code-attribute>文本 = 选项1 > < / asp:ListItem >
< asp:ListItem = 2 文字 = 选项2 > < / asp:ListItem >
< asp:ListItem = 3 文字 = 选项3 > < / asp:ListItem >
< asp:ListItem Value = 4 < span class =code-attribute>文本 = 选项4 > < / asp:ListItem >
< / a sp:DropDownList > < / p >
< p > < asp: TextBox runat = server id = txtg4action > < / asp:TextBox > < / p >
< p > < asp:按钮 runat = server ID = btnGo OnClick = btnGo_Click 文字 = 提交 / > < / p >
< p > < asp:Literal runat = server id < span class =code-keyword> = litX / > < / p >
< / form >
< / body >
< / html >


Changing the value of a textbox via javascript on dropdownlist change (no postback). Textbox updates properly but the value does not come through when the form is submitted. I assume this is because the javascript change is only happening on the client side? How can I get this value on server side?

What I have tried:

<pre>  <script type="text/javascript">
      function ddlChange() {
          var ddl = document.getElementById('<%=DDLg4corp.ClientID %>');
          var textBox = document.getElementById('<%= txtg4action.ClientID%>');

          textBox.value = ddl.options[ddl.selectedIndex].value;
      }
</script>

<asp:DropDownList runat="server" ID="DDLg4corp" onchange="ddlChange();">
  <asp:ListItem Value="1" Text="Option 1"></asp:ListItem>
  <asp:ListItem Value="2" Text="Option 2"></asp:ListItem>
  <asp:ListItem Value="3" Text="Option 3"></asp:ListItem>
  <asp:ListItem Value="4" Text="Option 4"></asp:ListItem>
</asp:DropDownList>

<asp:TextBox runat="server" id="txtg4action"></asp:TextBox> 



 Private Sub Button_Click(sender As Object, e As EventArgs) Handles btnSubmit1.Click,


With update
  .Connection = connection
  .CommandType = CommandType.Text
    If qrtSelect.SelectedValue = 1 Then
       .CommandText = "Update table set txtg4action=@txtg4action where users=@users"
       .Parameters.AddWithValue("@txtg4action", txtg4action.Text)

解决方案

Well, as promised, here's a simple page using your code:

<%@ Page Language="VB" %>
<script runat="server"> 
    Private Sub btnGo_Click(ByVal Sender As Object, ByVal e As EventArgs)
      litX.Text = "You selected " & txtg4action.Text
    End Sub
</script>
<html>
<head>
   <title></title> 
   <script type="text/javascript">
      function ddlChange() {
         var ddl = document.getElementById('<%=DDList.ClientID %>');
            var textBox = document.getElementById('<%= txtg4action.ClientID%>');
            textBox.value = ddl.options[ddl.selectedIndex].value;
         }
   </script>
</head>
<body>
    <form id="Form1" runat="server"> 	   
      <p><asp:DropDownList runat="server" ID="DDList" onchange="ddlChange();">
        <asp:ListItem Value="1" Text="Option 1"></asp:ListItem>
        <asp:ListItem Value="2" Text="Option 2"></asp:ListItem>
        <asp:ListItem Value="3" Text="Option 3"></asp:ListItem>
        <asp:ListItem Value="4" Text="Option 4"></asp:ListItem>
      </asp:DropDownList></p>
      <p><asp:TextBox runat="server" id="txtg4action"></asp:TextBox></p>  
      <p><asp:Button runat="server" ID="btnGo" OnClick="btnGo_Click" Text="Submit" /></p>
	  <p><asp:Literal runat="server" id="litX" /></p>	
    </form>
</body>
</html>


这篇关于使用javascript更改后获取文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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