如何遍历页面源中的所有文本框元素并查找所有这些文本框的名称?

时间:2016-04-19 05:41:42

标签: javascript jquery html asp.net ajax

我正在编写一个程序,如果源页面作为输入给出,它将在网页中输出GUI元素的数量,并且还应输出所有文本框的名称。

我写的代码是:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My App</title>
    <link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<textarea id="TextArea1"></textarea>
        <br />
        <input id="Submit1" type="submit" value="submit" /></div>

    <script>
            var $textarea = $('#TextArea1'), $submit = $('#Submit1');
            $submit.click(function (e) {
                e.preventDefault();
                sourceCode = $textarea.val();
                var $searchObject = $('<div id="Searching"></div>');
                $searchObject.append($(sourceCode));


                //Search Tags


                $(function () {
                    var values = $('input[type=text]').map(function () {
                        return this.name
                    }).get()
                    alert(values);
                })

                alert("Name of text field = " + $searchObject.find('[type=text]').attr('name'));
                alert("Number of text boxes = " + $searchObject.find('[type=text]').length);
                alert("Number of Submit Buttons = " + $searchObject.find('[type=submit]').length);
                alert("Number of Telephone entry fields = " + $searchObject.find('[type=tel]').length);
                alert("Number of Password boxes = " + $searchObject.find('[type=password]').length);
                alert("Number of check boxes = " + $searchObject.find('[type=checkbox]').length);
                alert("Number of Radio buttons on page = " + $searchObject.find('[type=radio]').length);
                alert("Number of drop down lists = " + $searchObject.find('select').length);
                alert("Number of Images on page = " + $searchObject.find('img').length);
                alert("Number of Hyperlinks on page = " + $searchObject.find('a[href]').length);
                alert("Number of Buttons     = " + $searchObject.find('[type=button]').length);
                alert("Number of Date entry fields= " + $searchObject.find('[type=date]').length);
    });
        </script>
</body>
</html>

当我运行此代码时,我的结果始终为'domain':

$(function () {
                    var values = $('input[type=text]').map(function () {
                        return this.name
                    }).get()
                    alert(values);
              })

我将Google注册页面的源代码作为输入。 任何人都可以帮我一个代码,它将遍历所有文本框并列出他们的名字吗?

4 个答案:

答案 0 :(得分:0)

这是最简单最有效的方法:

<ion-tabs class="tabs-positive tabs-icon-top">
<ion-tab title="IN" icon-on="ion-arrow-down-a" icon-off="ion-arrow-up-a">
    <ion-nav-view name="menuContent">
        Tab content 1
    </ion-nav-view>
</ion-tab>
<ion-tab title="OUT" icon-on="ion-arrow-up-a" icon-off="ion-arrow-up-a">
    Tab content 2
</ion-tab>
<ion-tab title="ENQUIRY" icon-on="ion-document-text" icon-off="ion-document-text">
    Tab content 3
</ion-tab>

答案 1 :(得分:0)

要获取所有输入文本类型元素的名称,您需要遍历所有文本类型元素:

$searchObject.find('[type=text]').each(function() {
     alert($(this).attr("name"));
});

这将为您提供所有文本类型输入元素的名称。

答案 2 :(得分:0)

var textBoxList={}; //object
var textBoxListARR=[] //array

$.each($("input[type='text']"),function(i,e){
    textBoxListARR.push(e.name); //create array
    textBoxList[e.id]=e.name; //create object
});

//resutls
console.log(textBoxList);
console.log(textBoxListARR);

答案 3 :(得分:0)

$('body').find("input[type='text']").each(function (index) {
   //get name or anything whatever you want.

});

这里的body是父元素