【问题标题】:Mismatch of text in drop down list and textbox text下拉列表和文本框文本中的文本不匹配
【发布时间】:2013-01-25 06:45:51
【问题描述】:

我有下拉列表和一个文本框

我正在从下拉列表中选择一个项目,并在选择时从下拉列表中选择的文本显示在文本框中。

我在这里遇到如下问题:

下拉列表中填充的文本之一是:

SUNROOF, POWER, TILT-SLIDING, ELECTRIC with express-open and wind deflector -Includes (CJ2) Air conditioning, dual-zone, automatic.  -Includes (ZM9) Universal Transmitter)

But when this is selected and the same text shown in textbox, the text is becoming as below .. i.e losing a space, and this is causing a problem.

SUNROOF, POWER, TILT-SLIDING, ELECTRIC with express-open and wind deflector -Includes (CJ2) Air conditioning, dual-zone, automatic. -Includes (ZM9) Universal Transmitter)

注意:下拉列表中的“自动。[此处的 2 个空格]-包含”和文本框中的“自动。[此处的 1 个空格]-包含”。即少一个空格。

文本被加载到下拉列表中,如下所示:

ddlEngine.Items.Add(new ListItem(Engine[i][0], Engine[i][1]));

这里引擎[i][0] = TEXT

Engine[i][1] = 用于差异目的的整数值..

并且在下拉列表值更改时...文本被复制到文本框中,如下所示:

document.getElementById("engineText").value = document.getElementById("ddlEngine").options[document.getElementById("ddlEngine").selectedIndex].text;

我需要这两个值相同。 知道为什么会这样吗???以及我如何才能了解这种行为。

【问题讨论】:

  • 您为什么要尝试在下拉列表中填充那么多以 James.. 开头的文本?
  • 好吧,那是有道理的.. 那么下拉列表中的值在哪里或如何填充..?您是否有权访问代码,或者您可以显示它是如何加载的..?
  • @DJKRAZE .. 请查看编辑一次..我添加了所需的代码..谢谢

标签: c# asp.net .net drop-down-menu textbox


【解决方案1】:

我刚刚尝试过,之后在代码隐藏中:

ddl1.Items.Add("12        34")

文本在页面的下拉列表中出现“12 34”。

如果可行,您可以在将字符串添加到下拉列表之前将所有多个空格转换为单个空格。

另一种方法是使用 ddlEngine.SelectedIndex 复制原始文件,如下所示:

TextBox1.Text = Engine[ddlEngine.SelectedIndex][0]

【讨论】:

  • 当然,我将不得不采用您建议的方法...没有其他方法可以解决这个问题吗?
  • 也许您可以使用 ddlEngine.SelectedIndex 来复制原件? textbox.text = Engine[ddlEngine.SelectedIndex][0] 之类的东西(我编辑了答案以更好地展示这一点。)
【解决方案2】:

只是想确认你的两个控件是否都是asp.net控件?

我刚刚复制了您的代码并执行了。它对我来说很好用。

<asp:DropDownList ID="DropDownList1" runat="server" Width="1108px" AutoPostBack="True"
            onchange="changeCursor()" Height="26px">
     <asp:ListItem>SUNROOF, POWER, TILT-SLIDING, ELECTRIC with express-open and wind deflector -Includes (CJ2) Air conditioning, dual-zone, automatic.  -Includes (ZM9) Universal Transmitter)</asp:ListItem>
     <asp:ListItem>select</asp:ListItem>
</asp:DropDownList>

<asp:TextBox ID="TextBox1" runat="server" Width="1103px"></asp:TextBox>

以下是显示所选值的 javascript 函数-

<script language="javascript" type="text/javascript">
    function changeCursor() {
        document.getElementById("TextBox1").value = document.getElementById("DropDownList1").options[document.getElementById("DropDownList1").selectedIndex].text;
    }
</script>

你是不是用同样的方法????

【讨论】:

  • 下拉列表&lt;asp:DropDownList ID="ddlEngine" width="300" CssClass="textboxda" AutoPostBack="True" runat="server" onmousewheel="return false;" /&gt;文本框&lt;asp:textbox ID="engineText" TextMode="MultiLine" ReadOnly="true" Rows="4" height="50px" Columns="56" CssClass="textboxda" runat="server" /&gt;
  • 是的..它是相同的方式..但是当文本加载到文本框中时我会删除一个空格...
  • 你在哪里写 document.getElementById("engineText").value = document.getElementById("ddlEngine").options[document.getElementById("ddlEngine").selectedIndex].text;跨度>
  • 我认为添加到下拉列表时空格被删除。
猜你喜欢
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多