Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查页面中的任何输入或textarea元素是否为空_Javascript_Jquery - Fatal编程技术网

Javascript 检查页面中的任何输入或textarea元素是否为空

Javascript 检查页面中的任何输入或textarea元素是否为空,javascript,jquery,Javascript,Jquery,我在我的网站上添加了一个机制,当用户要关闭他们正在工作的页面时,它应该会提醒用户。我将一个函数dropDraftConfirmation绑定到事件,如下所示: window.onbeforeunload = dropDraftConfirmation; function dropDraftConfirmation() { if (<there is an input or textarea element not empty on the page>) {

我在我的网站上添加了一个机制,当用户要关闭他们正在工作的页面时,它应该会提醒用户。我将一个函数
dropDraftConfirmation
绑定到事件,如下所示:

window.onbeforeunload = dropDraftConfirmation;

function dropDraftConfirmation()
{
    if (<there is an input or textarea element not empty on the page>) {
        alert('Your changes will be lost. Are you sure you want to exit the page?');
    }
}
if ($("input[value!='']").length > 0 || $('textarea:not(:empty)').length > 0 ){
window.onbeforeunload=dropDraftConfirmation;
函数dropDraftConfirmation()
{
如果(){
警报('您的更改将丢失。是否确实要退出该页?');
}
}

但每次我关闭页面时都会调用此函数。所以我的问题是,如何检测页面中是否有输入或textarea元素不是空的?顺便说一下,我正在使用。

在卸载之前,像下面那样重写您的函数,以检查任何未保存的更改

window.onbeforeunload = checkUnSaved;

function checkUnSaved(){
     if($("#textarea").val() === ""){
         dropDraftConfirmation();
     }else
         return;
}

function dropDraftConfirmation()
{
    if (<there is an input or textarea element not empty on the page>) {
        alert('Your changes will be lost. Are you sure you want to exit the page?');
    }
}
window.onbeforeunload=checkunsave;
函数checkunsave(){
if($(“#textarea”).val()=“”){
dropDraftConfirmation();
}否则
返回;
}
函数dropDraftConfirmation()
{
如果(){
警报('您的更改将丢失。是否确实要退出该页?');
}
}

我认为这应该可以解决问题

window.onbeforeunload = dropDraftConfirmation;

function dropDraftConfirmation()
{
    if ($("input:empty,textarea:empty").length == 0) {
        alert('Your changes will be lost. Are you sure you want to exit the page?');
    }
}

您还可以执行以下操作:

var errors = 0;

    $("input, textarea").map(function(){
        var val = $.trim( $(this).val() );
        if( val.length < 1 ) {
           errors++;  
        }
    });

    if( errors > 0 ) {
        alert('Your changes will be lost. Are you sure you want to exit the page?');
    }
var错误=0;
$(“输入,文本区域”).map(函数(){
var val=$.trim($(this.val());
如果(值长度<1){
错误++;
}
});
如果(错误>0){
警报('您的更改将丢失。是否确实要退出该页?');
}

您的情况如下所示:

window.onbeforeunload = dropDraftConfirmation;

function dropDraftConfirmation()
{
    if (<there is an input or textarea element not empty on the page>) {
        alert('Your changes will be lost. Are you sure you want to exit the page?');
    }
}
if ($("input[value!='']").length > 0 || $('textarea:not(:empty)').length > 0 ){
我最后使用了
if($('textarea').val()| |$('input').val(){alert(您所做的更改将丢失。是否确实要退出页面?);}
,但谢谢