简体   繁体   English

在jQuery中将dropdownlist值设置为textbox

[英]set dropdownlist value to textbox in jQuery

If my dropdown has a value selected, I want to show the selected item text in my textbox. 如果我的下拉列表中选择了一个值,我想在文本框中显示所选的项目文本。 If not, I want to empty it. 如果没有,我想清空它。

<asp:DropDownList ID="ddl"  runat="server" AutoPostBack="true" onselectedindexchanged="ddlSelectedIndexChanged" Width="200px" onchange="ddlChange()">
</asp:DropDownList>
<asp:TextBox ID="hdntxtbxTaksit" runat="server" Visible="false"></asp:TextBox>

How can I do this? 我怎样才能做到这一点?

If you want to assign selected value of dropdownlist to text box you can assign in change event without and condition as change event means dropdown have been selected . 如果要将下拉列表的选定值分配给文本框,则可以在更改事件中指定不带和条件,因为change event means dropdown have been selected

With javascript 用javascript

Change 更改

onchange="ddlChange()"

To

onchange="ddlChange(this)"

Your javascrpt method would be 你的javascrpt方法会是

function ddlChange(ddl)
{      
      document.getElementById('<%= hdntxtbxTaksit.ClientId %>').value = this.value;
}

With jQuery 用jQuery

Remove onchange="ddlChange()" as we will bind event with jquery. 删除onchange =“ddlChange()”,因为我们将使用jquery绑定事件。

$('<%= ddl.ClientId %>').change(function(){          
          $('<%= hdntxtbxTaksit.ClientId %>').val($(this).val());    
});

Using jQuery; 使用jQuery;

$().ready(function(){           
  $('#<%=ddl.ClientID %>').change(function () {               
    $('#<%=hdntxtbxTaksit.ClientID %>').val($(this).val() == "0" ? "" : $(this).val());              
  });
});

EDIT: With above method, you don't need to call onchange event of the dropdown. 编辑:使用上述方法,您不需要调用下拉列表的onchange事件。 So your markup can be 所以你的标记可以

<asp:DropDownList ID="ddl"  runat="server" AutoPostBack="true"
         onselectedindexchanged="ddlSelectedIndexChanged" Width="200px">
</asp:DropDownList>
<asp:TextBox ID="hdntxtbxTaksit" runat="server" Visible="false"></asp:TextBox>

you can do it like this 你可以这样做

function ddlChange()
{
   if(document.getElementById('<%= ddl.ClientId %>').value=='0')
   {
      document.getElementById('<%= hdntxtbxTaksit.ClientId %>').value = "";
   }
   else
   {
      document.getElementById('<%= hdntxtbxTaksit.ClientId %>').value = document.getElementById('<%= ddl.ClientId %>').value;    
   }
}

Try this code and remove onchange event added in your asp:dropdownlist 尝试此代码并删除在asp:dropdownlist中添加的onchange事件

Note: AUTOPOSTBACK property will always do a page submit, it wont stop even if you return false in onchange method of dropdown 注意: AUTOPOSTBACK属性将始终执行页面提交,即使您在ondown方法中返回false,它也不会停止

        $(document).ready(function () {
            $('#<%=ddl.ClientID %>').change(function () {

                if ($(this).val() != "0") {
                    $('#<%= hdntxtbxTaksit.ClientID %>').val($(this).val());
                }
                else {
                    $('#<%= hdntxtbxTaksit.ClientID %>').val("");
                }
            });

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM