javascript - 如何在 aspx 页面加载时获取文本框值

标签 javascript jquery asp.net

伙计们,我有两个页面GridviewWithJquery.aspxEkle.aspx

在第一个文件中,当我单击 Ekle 按钮时,我将 Ekle.aspx 加载到 div 中(使用 id="content" )。

之后我尝试获取位于 Ekle.aspx 中的文本框值.

我无法使用正常语法获取值 ( $("#ekle_gonder").click(function){...}; )并且它没有进入点击函数,也没有获取文本框值。

我怎样才能得到这些值?

下面是我的代码:

GridviewWithJquery.aspx:

<script>
    $(document).ready(function () {

        var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
        var name = "";
        printValues();

        function printValues() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "GridviewWithJquery.aspx/fnc",
                data: "{}",
                dataType: "json",
                success: function (result) {
                    $("#gw1").find("td").remove();
                    for (var i = 0; i < result.d.length; i++) {
                        $("#gw1").append("<tr><td style='width:150px;'>" + result.d[i].Id + "</td><td style='width:150px;'>" + result.d[i].Name
                            + "</td><td style='width:150px;'>" + result.d[i].Surname + "</td><td style='width:150px;'>" + result.d[i].Sex
                            + "</td><td style='width:150px;'>" + result.d[i].Email + "</td><td style='width:150px;'>" + result.d[i].City
                            + "</td><td style='width:150px;'>" + result.d[i].Age + "</td></tr>");
                    }
                },
                error: function (result) {
                    alert("Unexpected result occured!");
                },
                complete: function () {

                    setTimeout(printValues,5000);
                }

            });
        }

        $(function () {
            $("#div_sil").dialog();

            $("#sil_name_error").dialog({
                autoOpen: false,
                buttons: [{
                    text: "OK",
                    click: function () { $(this).dialog("close"); }
                }]
            });
        })

        $("#ekle_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Ekle.aspx #ekle");

        });
        $("#sil_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Sil.aspx #div_sil");

        });
        $("#guncelle_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Guncelle.aspx #div_guncelle");

        });

        $(function () {
            $("#content").dialog({ autoOpen: false });
        });
    });

</script>

<body>
<form id="form1" runat="server">

    <div id="div_goruntule">

        <asp:GridView ID="gw1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2"
            HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
            <Columns>
                <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Surname" HeaderText="Surname" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Sex" HeaderText="Sex" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Email" HeaderText="Email" />
                <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Age" HeaderText="Age" />
            </Columns>
        </asp:GridView>

    </div>

    <div id="buttons">
        <asp:Table ID="Table1" runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <input type="button" id="ekle_button" value="Ekle" />
                    <input type="button" id="sil_button" value="Sil" />
                    <input type="button" id="guncelle_button" value="Guncelle" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </div>

    <div id="content">
    </div>

    <div id="ekle_part">
        <div id="ekle_id_error">
            <p>Please enter a valid id number.Id should include <b>only</b> numbers.</p>
        </div>
        <div id="ekle_name_error">
            <p>Please enter a valid name.Name should include <b>only</b> characters, not numbers.</p>
        </div>
        <div id="ekle_surname_error">
            <p>Please enter a valid surname.Surname should include <b>only</b> characters, not numbers.</p>
        </div>
        <div id="ekle_email_error">
            <p>Please enter a valid email address.Email format should be xxxxx@xxxxx.xxx</p>
        </div>
        <div id="ekle_city_error">
            <p>Please enter a valid city.City name should include <b>only</b> characters, not numbers</p>
        </div>
        <div id="ekle_age_error">
            <p>Please enter a valid age number.Age should be include <b>only</b> numbers.</p>
        </div>
    </div>

    <div id="sil_part">
        <div id="sil_name_error">
            <p>Name can not include numeric values.</p>
        </div>
    </div>

    <div id="guncelle_part">
        <div id="guncelle_id_error">Id contains only numbers.</div>
        <div id="guncelle_name_error">Name can not be empty and can not be include numeric values.</div>
        <div id="guncelle_surname_error">Surname can not be empty and can not include numeric values.</div>
        <div id="guncelle_email_error">Email format should be like xxxxxx@xxxx.com</div>
        <div id="guncelle_city_error">City can not be empty and can not be include numeric values.</div>
        <div id="guncelle_age_error">Age contains only numbers</div>
    </div>

</form>

下面是ekle.js:

$(document).ready(function () {

var result = 1;
var id, name, surname, email, city, age;

$("#ekle_gonder").click(function () {

    var gender = document.getElementById('<%= ddl1.ClientID%>');
    var gender2 = gender.options[gender.selectedIndex].value;
    check();
    var obj = {};
    obj.Id = id;
    obj.Name = name;
    obj.Surname = surname;
    obj.Sex = gender2;
    obj.Email = email;
    obj.City = city;
    obj.Age = age;

    if (result == 1) {
        $.ajax({
            type: "post",
            url: "GridviewWithJquery.aspx/ekle_func",
            contentType: "application/json;charset:utf-8",
            data: JSON.stringify(obj),
            dataType: "json",
            success: onSuccess,
            error: function (xhr, status, error) {
                alert("no");
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
        });
    }

});



function check() {

    var mailregex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
    var numregex = /(^\d+$)/;

    id = $("#txt1").val();
    name = $("#txt2").val();
    surname = $("#txt3").val();
    email = $("#txt5").val();
    city = $("#txt6").val();
    age = $("#txt7").val();

    if ((numregex.test(parseInt(id)) == false) || (parseInt(id) < 0)) {
        open_error_box("#ekle_id_error", "#txt1");
    }
    if ((name == '') || (textregex.test(name) == false)) {
        open_error_box("#ekle_name_error", "#txt2");
    }
    if ((surname == '') || (textregex.test(surname) == false)) {
        open_error_box("#ekle_surname_error", "#txt3");
    }
    if ((email == '') || (mailregex.test(email) == false)) {
        open_error_box("#ekle_email_error", "#txt5");
    }
    if ((city == '') || (textregex.test(city) == false)) {
        open_error_box("#ekle_city_error", "#txt6");
    }
    if ((numregex.test(parseInt(age)) == false) || (parseInt(age) < 0) || (parseInt(age) > 100)) {
        open_error_box("#ekle_age_error", "#txt7");
    }
    return result;
}

function open_error_box(error_name, name) {
    $(error_name).dialog("open");
    $(name).val('');
    result = 0;
}

function onSuccess() {

    $("#txt1").val('');
    $("#txt2").val('');
    $("#txt3").val('');
    $("#txt4").val('');
    $("#txt5").val('');
    $("#txt6").val('');
    $("#txt7").val('');
}

$(function () {
    $("#ekle").dialog();

    $("#ekle_id_error ,#ekle_name_error, #ekle_surname_error, #ekle_email_error, #ekle_city_error, #ekle_age_error").dialog(
    {
        autoOpen: false,
        buttons: [{
            text: "OK",
            click: function () { $(this).dialog("close"); }
        }]
    });
})
});

我被困了几个小时。

请告诉我一种获取 GridviewWithJquery.aspx 中加载项目的值的方法页面?

最佳答案

I can't get the value with normal syntax ( $("#ekle_gonder").click(function){...}; ) and it didn't enter the click function nor get the textbox values.

那是因为您引用了错误的 ID。

在 HTML 中,您获得了以下按钮。

<input type="button" id="ekle_button" value="Ekle" />

注意 ID 为 ekle_button .

在你的 JS 上,你试图绑定(bind) $("#ekle_gonder").click(function () {

什么时候应该是:

$("#ekle_button").click(function () {

关于javascript - 如何在 aspx 页面加载时获取文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33170665/

相关文章:

javascript - JQuery 一次切换两个类

asp.net - 创建 .NET Core 1.1 Web 应用程序时出现问题

asp.net - System.Web.UI.Control.LoadRecursive() - 堆栈不足,无法继续安全地执行程序。

javascript - 谷歌图表 :Wrong result of 'select' event after filtered with ControlWrapper

javascript - 从本地文件夹响应 native 调用动态图像

javascript - 将值写入 div 时出现 insideHTML 问题

javascript - 未选中时如何屏蔽表单输入字段?

javascript - Highcharts 4.0.3 堆积条形图 bug 添加了 â-

javascript - 如何将数组的元素作为参数传递给 JavaScript 中的函数?

c# - 连接和 Url 编码字符串时 Base-64 字符串中的无效字符