Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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-编辑具有特定Id的第一个空输入_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript-编辑具有特定Id的第一个空输入

Javascript-编辑具有特定Id的第一个空输入,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我正在为我的站点创建一个表单,我想创建一些类似于自定义属性“required”的东西。我有一些Id为“必需”的输入。比如: <input type="text" id="required"> <input type="text" id="required"> <input type="text" id="required"> Javascript: $('#check').click(function() { var x = $("input[id=

我正在为我的站点创建一个表单,我想创建一些类似于自定义属性“required”的东西。我有一些Id为“必需”的输入。比如:

<input type="text" id="required">
<input type="text" id="required">
<input type="text" id="required">
Javascript:

$('#check').click(function() {
    var x = $("input[id=required]").filter(function() {
        return $(this).val().trim() == '';
    });

    $(x).each(function (index) {
        $(this).val('');
        $(this).addClass("input-error");
        $(this).attr({
          "data-toggle": "popover",
          "data-container": "body",
          "data-content": "Wypełnij to pole",
          "data-trigger": "manual"
        });
        $(this).popover('show');  
        $(this).keypress(function() {
            $(this).removeClass("input-error");
            $(this).popover('destroy'); 
        });
    });

    if(x.length > 0) {
        $('html, body').animate({
            scrollTop: $(x[0]).offset().top-100
        }, 200);
    }

});
JSFIDLE

HTML俱乐部的第一条规则,永远不要重复id。使用类而不是id对元素进行分组
$('#check').click(function() {
    var x = $("input[id=required]").filter(function() {
        return $(this).val().trim() == '';
    });

    $(x).each(function (index) {
        $(this).val('');
        $(this).addClass("input-error");
        $(this).attr({
          "data-toggle": "popover",
          "data-container": "body",
          "data-content": "Wypełnij to pole",
          "data-trigger": "manual"
        });
        $(this).popover('show');  
        $(this).keypress(function() {
            $(this).removeClass("input-error");
            $(this).popover('destroy'); 
        });
    });

    if(x.length > 0) {
        $('html, body').animate({
            scrollTop: $(x[0]).offset().top-100
        }, 200);
    }

});