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

标签 javascript jquery asp.net

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

<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>

关于javascript - 单击按钮时,循环遍历文本框 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863043/

相关文章:

javascript - 在 Google map 的 JS API 中,根据事件, fromPointToLatLng 似乎不准确

javascript - 投资组合的模拟元素

javascript - 对复选框选择进行分组

javascript - 模拟所有视口(viewport)大小的恒定页面大小

asp.net - 将 SQL 数据库导出到 Access - ASP.NET

c# - 需要将 C# 字符串传递给 javascript 函数

javascript - 创建一个使用新值更新 dom 元素的 setter?

javascript - 显示更多/显示更少 CSS JS 动画不起作用

jquery - Bootstrap 选项卡 : load NGL viewer into tab

ASP.net 图表控件 : hide all lines (axes, 等)数据点除外