动态生成的HTML中,jQuery的val方法返回undefined。

huangapple go评论99阅读模式
英文:

jQuery val undefined for dynamically generated HTML

问题

我使用以下代码动态生成输入字段,我想在jQuery UI对话框中解析这些字段。首先,用户输入系统。点击“确认系统”按钮后,为每个系统生成新的输入字段。现在,我想访问动态生成的输入字段中输入的数据。然而,这些值始终为undefined。

以下是代码示例:

$(document).ready(function() {
  var systemsArray;
  var div = $("<div id=\"dialog\"></div>");
  div.append("<p>" +
    "<label for=\"systems\">Systems:</label>" +
    "<input id=\"systems\" size=40></input>" +
    "<button id=\"systems_btn\">Confirm systems</button>" +
    "</p>");
  $("body").append(div);
  $("#dialog").dialog({
    autoOpen: true,
    modal: true,
    width: 800,
    height: 500,
    buttons: [{
      text: "Execute",
      click: function() {
        $("#items").children(".input").each(function() {
          alert(this.val().join(", ") + ". "); // <-- val undefined
        });
        $(this).dialog("close");
      }
    }]
  });
  $("#systems_btn").click(function() {
    $("#items").empty();
    var fieldSet = $("<fieldset id=\"items\"></fieldset>");
    systemsArray = $("#systems").val().split(",");
    for (const systemID in systemsArray) {
      var label = $("<label for=\"" + systemID + "\">" + "Items for " + systemsArray[systemID] + ":" + "</label>");
      var input = $("<input id=\"" + systemID + "\" size=40></input>");
      fieldSet.append(label);
      fieldSet.append(input);
    }
    $(this).parent().append(fieldSet);
  });
});

为了清晰起见,这是在按下“执行”按钮之前对话框的外观(由于val()未定义而崩溃):
动态生成的HTML中,jQuery的val方法返回undefined。


<details>
<summary>英文:</summary>

I use the following code to dynamically generate input fields, which I want to parse in a jQuery UI dialog. First, the user enters systems. After a click on the &quot;Confirm systems&quot; button, new input fields for each system are generated. Now, I want to access the data entered in the dynamically generated input fields. However, these values are always undefined.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    $(document).ready(function() {
      var systemsArray;
      var div = $(&quot;&lt;div id=\&quot;dialog\&quot;&gt;&lt;/div&gt;&quot;);
      div.append(&quot;&lt;p&gt;&quot; +
        &quot;&lt;label for=\&quot;systems\&quot;&gt;Systems:&lt;/label&gt;&quot; +
        &quot;&lt;input id=\&quot;systems\&quot; size=40&gt;&lt;/input&gt;&quot; +
        &quot;&lt;button id=\&quot;systems_btn\&quot;&gt;Confirm systems&lt;/button&gt;&quot; +
        &quot;&lt;/p&gt;&quot;);
      $(&quot;body&quot;).append(div);
      $(&quot;#dialog&quot;).dialog({
        autoOpen: true,
        modal: true,
        width: 800,
        height: 500,
        buttons: [{
          text: &quot;Execute&quot;,
          click: function() {
            $(&quot;#items&quot;).children(&quot;.input&quot;).each(function() {
              alert(this.val().join(&quot;, &quot;) + &quot;. &quot;); // &lt;-- val undefined
            });
            $(this).dialog(&quot;close&quot;);
          }
        }]
      });
      $(&quot;#systems_btn&quot;).click(function() {
        $(&quot;#items&quot;).empty();
        var fieldSet = $(&quot;&lt;fieldset id=\&quot;items\&quot;&gt;&lt;/fieldset&gt;&quot;);
        systemsArray = $(&quot;#systems&quot;).val().split(&quot;,&quot;);
        for (const systemID in systemsArray) {
          var label = $(&quot;&lt;label for=\&quot;&quot; + systemID + &quot;\&quot;&gt;&quot; + &quot;Items for &quot; + systemsArray[systemID] + &quot;:&quot; + &quot;&lt;/label&gt;&quot;);
          var input = $(&quot;&lt;input id=\&quot;&quot; + systemID + &quot;\&quot; size=40&gt;&lt;/input&gt;&quot;);
          fieldSet.append(label);
          fieldSet.append(input);
        }
        $(this).parent().append(fieldSet);
      });
    });

&lt;!-- language: lang-html --&gt;

    &lt;link href=&quot;//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css&quot; rel=&quot;stylesheet&quot; /&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;//code.jquery.com/ui/1.11.2/jquery-ui.js&quot;&gt;&lt;/script&gt;

&lt;!-- end snippet --&gt;


 For clarity, this is what the dialog looks like before pressing the &quot;Execute&quot; button (which crashes, because val() is undefined):
[![Screenshot of dialog][1]][1]


  [1]: https://i.stack.imgur.com/zT8Av.png

</details>


# 答案1
**得分**: 1

有两个问题:

1.:`.children(".input")`会选择具有类名为input的元素,但在你的代码中没有这样的元素,所以我将其更改为`.children("input")`,这将选择类型为input的元素。

2.:`this.val()`不起作用,因为.val()是一个jQuery方法,我将其更改为`$(this).val()`,即给DOM元素添加一个jQuery包装器,然后警告框将显示输入的值;-)

```javascript
$(document).ready(function() {
  var systemsArray;
  var div = $("<div id=\"dialog\"></div>");
  div.append("<p>" +
    "<label for=\"systems\">Systems:</label>" +
    "<input id=\"systems\" size=40></input>" +
    "<button id=\"systems_btn\">Confirm systems</button>" +
    "</p>");
  $("body").append(div);
  $("#dialog").dialog({
    autoOpen: true,
    modal: true,
    width: 800,
    height: 500,
    buttons: [{
      text: "Execute",
      click: function() {
        $("#items").children("input").each(function() {
          alert($(this).val());
        });
        $(this).dialog("close");
      }
    }]
  });
  $("#systems_btn").click(function() {
    $("#items").empty();
    var fieldSet = $("<fieldset id=\"items\"></fieldset>");
    systemsArray = $("#systems").val().split(",");
    for (const systemID in systemsArray) {
      var label = $("<label for=\"" + systemID + "\">" + "Items for " + systemsArray[systemID] + ":" + "</label>");
      var input = $("<input id=\"" + systemID + "\" size=40></input>");
      fieldSet.append(label);
      fieldSet.append(input);
    }
    $(this).parent().append(fieldSet);
  });
});
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
英文:

There are two things:

1.: .children(&quot;.input&quot;) would select elements with the class input, thats not the case in your code, so i changed to .children(&quot;input&quot;) which will select elements of type input

2.: this.val() will not work as .val() is a jquery method, i changed it to $(this).val() e.g adding a jquery wrapper to the domelement and voilâ, the alert shows the entered value 动态生成的HTML中,jQuery的val方法返回undefined。

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

$(document).ready(function() {
  var systemsArray;
  var div = $(&quot;&lt;div id=\&quot;dialog\&quot;&gt;&lt;/div&gt;&quot;);
  div.append(&quot;&lt;p&gt;&quot; +
    &quot;&lt;label for=\&quot;systems\&quot;&gt;Systems:&lt;/label&gt;&quot; +
    &quot;&lt;input id=\&quot;systems\&quot; size=40&gt;&lt;/input&gt;&quot; +
    &quot;&lt;button id=\&quot;systems_btn\&quot;&gt;Confirm systems&lt;/button&gt;&quot; +
    &quot;&lt;/p&gt;&quot;);
  $(&quot;body&quot;).append(div);
  $(&quot;#dialog&quot;).dialog({
    autoOpen: true,
    modal: true,
    width: 800,
    height: 500,
    buttons: [{
      text: &quot;Execute&quot;,
      click: function() {
        $(&quot;#items&quot;).children(&quot;input&quot;).each(function() {
          alert($(this).val());
        });
        $(this).dialog(&quot;close&quot;);
      }
    }]
  });
  $(&quot;#systems_btn&quot;).click(function() {
    $(&quot;#items&quot;).empty();
    var fieldSet = $(&quot;&lt;fieldset id=\&quot;items\&quot;&gt;&lt;/fieldset&gt;&quot;);
    systemsArray = $(&quot;#systems&quot;).val().split(&quot;,&quot;);
    for (const systemID in systemsArray) {
      var label = $(&quot;&lt;label for=\&quot;&quot; + systemID + &quot;\&quot;&gt;&quot; + &quot;Items for &quot; + systemsArray[systemID] + &quot;:&quot; + &quot;&lt;/label&gt;&quot;);
      var input = $(&quot;&lt;input id=\&quot;&quot; + systemID + &quot;\&quot; size=40&gt;&lt;/input&gt;&quot;);
      fieldSet.append(label);
      fieldSet.append(input);
    }
    $(this).parent().append(fieldSet);
  });
});

<!-- language: lang-html -->

&lt;link href=&quot;//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;//code.jquery.com/ui/1.11.2/jquery-ui.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

这是因为你使用的是JS对象,而不是jQuery对象,JS对象没有.val()方法。

尝试使用$(this).val()

英文:

That is because you are using not jQuery object, but JS object that does not have .val()

Try doing $(this).val()

huangapple
  • 本文由 发表于 2023年7月27日 15:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定