简体   繁体   English

使用javascript从动态表单元素中获取值

[英]getting values from dynamic form elements with javascript

I'm creating a dynamic form with javascript which contains a drop down list and an input text as below : 我正在使用javascript创建一个动态表单,其中包含下拉列表和输入文本,如下所示:

$(document).ready(function() {  
    document.write("<FORM>");

    document.write("<SELECT NAME='SelectMenu'>");
    for (var i = 1; i <= 3; i++)
    document.write("<OPTION>" +"one"+"</OPTION>");
    document.write("<OPTION>" +"two"+"</OPTION>");
    document.write("<OPTION>" +"three"+"</OPTION>");

    document.write('</SELECT>');
    document.write("<br>Entry  <input type='text' name='myInputs[]'>");
    document.write("<button onClick="ChoixDeQuestion()">Show</button>");

    document.write('</FORM>');
}); 

The problem here is that I can't access those fields since they don't even exist in the source code of the page. 这里的问题是我无法访问这些字段,因为它们甚至不存在于页面的源代码中。 I want to get the entered text value and the selected item of the list. 我想获取输入的文本值和列表中的选定项。 So Any idea please!! 所以任何想法请!!

Thanks 谢谢

Instead of using the basic syntax "document.write(...)", you should use dynamic elements and creating new HTML elements. 您应该使用动态元素并创建新的HTML元素,而不是使用基本语法“document.write(...)”。

Document.write only actually displays the text without really inserting it. Document.write实际上只显示文本而没有真正插入它。 If you want to edit your HTML later on, you need the element to be created and added to the document. 如果以后要编辑HTML,则需要创建元素并将其添加到文档中。

Using, for example, the "CreateElement" syntax. 例如,使用“CreateElement”语法。 Here's a good tutorial to get you started on how to create a form dynamically. 这是一个很好的教程,可以帮助您开始动态创建表单。 Afterwards you can append elements to it, and create many more elements that way. 之后,您可以向其追加元素,并以此方式创建更多元素。

Using document.write should be avoided. 应该避免使用document.write Its not a good practice. 这不是一个好习惯。

You are using jQuery and its very easy to dynamically create elements in jQuery. 您正在使用jQuery,它非常容易在jQuery中动态创建元素。

You can do something like this, 你可以这样做,

$(document).ready(function() {  
    var options = '';
    for (var i = 1; i <= 3; i++)
    options +='<option>one</option>';
    options +='<option>two</option><option>three</option>';
    var html = '<select>' + options + '</select><br>Entry <input type="text" name="myInputs[]" />';
    var button = $('<button>Show</button>').click(function(e){
        // Code to be executed when button is clicked

        e.preventDefault();  // since by default button submits the form
        alert('button is clicked');
    });
    $("<form></form>").append(html).append(button).appendTo('body');
});

jsFiddle 的jsfiddle

If you know a basic jQuery, everything is self explained, but do let me know if something bothers you :) 如果你知道一个基本的jQuery,一切都是自我解释,但如果有什么困扰你,请告诉我:)

If you're already using jQuery, make more use of it: 如果您已经在使用jQuery,请更多地使用它:

$(document).ready(function() {
    var form = $('<form />'),
    dropdown = $('<select />', {
        name: 'SelectMenu'
    }),
    textbox = $('<input />', {
        type: 'text',
        name: 'myInputs[]'
    }),
    button = $('<button />', {
        text: 'Show'
    }).on('click', ChoixDeQuestion);

    for (var i = 1; i <= 3; i++) {
        $('<option />', {
            text: i
        }).appendTo(dropdown);
    }

    form
        .append(dropdown)
        .append('<br />Entry')
        .append(textbox)
        .append(button)
        .appendTo('body');
});

This is creating all the nodes and inserting them into the DOM in a nice way; 这是创建所有节点并以一种很好的方式将它们插入DOM中; you can also just create one big string contents with your html, and then do this: 你也可以用你的html创建一个大的字符串contents ,然后执行以下操作:

$(contents).appendTo('body');

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

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