单击按钮时,循环遍历文本框 JavaScript

on button click, loop through text boxes javascript

本文关键字:文本 JavaScript 遍历 循环 按钮 单击      更新时间:2023-09-26

我正在尝试通过单击按钮来启动一个功能。单击时,函数循环遍历每个输入 type=text 元素,如果该变量 = null 或变量 = " 调用方法 confirm('您确定要保存空白行')。这是我的代码/伪代码:

<script type="text/javascript">
    function isInputEmpty() {
        $("#btnSave").click(function(){
            $('input[type=text]').each(function(){
                var x = $(this).attr('value');
                var bool = false;
                if(x == null || x == "")
                {
                    bool = true;
                }
                else
                {
                    send the request
                }
                if(bool == true)
                {
                    if(confirm('Are you sure you want to save a blank URL?'))
                    {
                        send the request
                    }
                    else
                    {
                        do nothing or cancel the request
                    }
                }
                else
                {
                    send the request
                }
             }
        }
</script>

这是我的asp按钮代码:

<asp:Button ID="btnSave" runat="server" Text="Save"/>

如果您需要更多信息,请告诉我。提前致谢

对于 ID 问题,如果您使用 ASP.Net 4.0 +,请设置ClientIDMode=Static

 <asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save"/>

.JS

<script type="text/javascript">
 function isInputEmpty() {
    $("#btnSave").click(function(){
        $('input[type=text]').each(function(){
            var x = this.value;
            var bool = false;
            if(x === null || x === "")
            {
                bool = true;
            }
            else
            {
                send the request
            }
            if(bool === true)
            {
                if(confirm('Are you sure you want to save a blank URL?'))
                {
                    LoadData();
                }
                else
                {
                    return;//do nothing
                }
            }
            else
            {
                send the request
            }
         }
    }

function LoadData()
{
$.ajax({
    type: "GET",
    url: 'page.aspx',       
    timeout: 1000,        
    success:function(data){    
     //do work
    },
    error:function(jqxhr, status){
     if(status==="error"){
     //handle error
     }    
 });
}
</script>
由于

它是 ASP.NET 的,该ID将呈现为不同的,请尝试获取客户端ID(如果JS在同一个文件中,如果不是,请使用唯一的类并通过它分配处理程序)

$("#<%=btnSave.ClientID%>").click(function() {
    $("input:text").each(function() {
        if (!this.value.length) {
            var confirm = confirm('are you sure you want to save a blank row')
            ..
        }
    });
});

你也可以像下面这样做。

<script type="text/javascript">
            function isInputEmpty() {
                var bool = false;
                $("#btnSave").click(function () {
                    $('input[type=text]').each(function () {
                        var x = $(this).attr('value');
                        if (x == null || x == "") {
                            bool = true;
                        }
                    });
                    if (bool == true) {
                        if (confirm('Are you sure you want to save a blank URL?')) {
                            //send the request
                        }
                        else {
                            //do nothing or cancel the request
                        }
                    }
                    else {
                        //send the request
                    }
                });
            }
        </script>

目前还不完全清楚您的问题是什么,但这里的其他答案理所当然地集中在 ID 问题上和更改元素 ID 的 .Net 网络表单上。

建议的其他解决方案很好,但还有另一种方法,那就是按部分 ID 搜索。当 clientIDmode 未设置为静态时(或者如果您使用的是 .Net 4.0 之前),.NET ID 将始终在下划线后附加原始 ID,因此您可以使用 jquery 找到您的元素,如下所示:

$("[id$='_btnSave']").click(function(){   ...