【问题标题】:how to set value dynamically created textbox in javascript如何在javascript中设置值动态创建的文本框
【发布时间】:2014-11-20 11:50:25
【问题描述】:

我正在使用 javascript 动态生成一个对话框(它是一个 div 元素),其中包含一个 textbox 。 问题:我可以很好地生成我的文本框,但我无法从中获得价值。 innerHTML 每次都返回空白。我不确定我做错了什么。

intTextBox = intTextBox + 1;
            var contentID = document.getElementById('content');
            var newTBDiv = document.createElement('div');
            newTBDiv.setAttribute('id', 'strText' + intTextBox);
            newTBDiv.innerHTML = String.fromCharCode(64 + (intTextBox + 2)) + ' :' 
                   + "<input style='margin-top:10px;margin-left:10px;width: 250px;'"
                   + " class='txt3_double' type='text' id='" 
                   + intTextBox + "' name='" + intTextBox 
                   + "' placeholder='Next Location Point'/>"
                   + "<img src='img/Add.png' onclick='addElement();return false;' />"
                   + "<img src='img/Minus.png' onclick='removeElement();return false;' />";
            contentID.appendChild(newTBDiv);
            textboxes = intTextBox;

            TextBoxloc = TextBoxloc + 1;
            var contentIDloc = document.getElementById('content');
            var newTBDivloc = document.createElement('div');
            newTBDivloc.setAttribute('id', 'strText' + TextBoxloc);
            newTBDivloc.innerHTML = String.fromCharCode(64 + (TextBoxloc + 2)) 
                       + ' :' + "<input class='txt3_double' type='text' id='" 
                       + TextBoxloc + "' name='" + TextBoxloc + "' />";
            contentIDloc.appendChild(newTBDivloc);
            textboxesloc = TextBoxloc ;

            $(document.getElementById(intTextBox)).autocomplete({
                source: function (request, response) {
                    debugger
                    $.ajax({
                        url: '<%=ResolveUrl("~/Root.aspx/GetAutoCompleteData") %>',
                        data: "{ 'Address': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            if (data.d.length == 0) {
                                $('.ui-autocomplete').hide();
                            }
                            else {
                                $(".pac-container").remove();
                                response($.map(data.d, function (item) {
                                    debugger
                                    return {
                                        label: item.split('-')[0],
                                        val: item.split('-')[1],
                                        long: item.split('-')[2]
                                    }
                                }))
                            }
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },

                select: function (e, i) {
                    debugger
                    document.getElementById(TextBoxloc).value = i.item.val;
                },
                minLength: 2

            });

【问题讨论】:

  • 哪一行给你带来了问题?当我不需要时,我不会阅读所有这些内容。
  • 如果你已经在使用jquery,为什么不使用它来生成没有任何问题的文本字段呢?

标签: javascript c# jquery html


【解决方案1】:

在您的代码中,我可以看到您在哪里创建具有来自 intTextBox 的唯一 id 的单个文本框,因此要获取文本框中的文本,只需使用以下值:

var textboxVal = $(intTextBox).value;

【讨论】:

    【解决方案2】:

    试试这个

    document.getElementById(TextBoxloc).value = i.item.value;

    i.item.val 应该是 i.item.value

    【讨论】:

      【解决方案3】:

      我看到您在 id 属性中添加“strText”并尝试使用

      document.getElementById(TextBoxloc).value = i.item.val;
      

      您是否在 i.item.val 中获得了正确的值??

      祝你好运……

      【讨论】:

      • 是的,我在 i.item.val 中获得了正确的值。 document.getElementById(TextBoxloc).value = i.item.val;它不工作。
      • 你试过 document.getElementById("strText" + TextBoxloc).value = i.item.val;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多