【问题标题】:Chrome value on load加载时的铬值
【发布时间】:2014-07-30 12:02:46
【问题描述】:

我的浏览器 Chrome 有一些问题,当它记住密码、用户名等时。我无法检查输入字段是否为空。 我使用以下代码,但是 if 语句在 Chrome 中不起作用,而在所有其他浏览器中都起作用。

$(document).ready(function() {    
    $('.inputWithLabel').each(function(){
        if ($(this).val().length != 0){
            var element_id = $(this).attr('id');
            $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
    });
});

我尝试更改以下标签

<form action="" method="post">
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="username">Brugernavn*</label>
        <input class="inputLabel inputWithLabel" id="username" name="username" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="password">Adgangskode*</label>
        <input class="inputLabel inputWithLabel" id="password" name="password" type="password">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="password_again">Gentag adgangskode*</label>
        <input class="inputLabel inputWithLabel" id="password_again" name="password_again" type="password">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="first_name">Fornavn*</label>
        <input class="inputLabel inputWithLabel" id="first_name" name="first_name" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="last_name">Efternavn</label>
        <input class="inputLabel inputWithLabel" id="last_name" name="last_name" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="email">Email*</label>
        <input class="inputLabel inputWithLabel" id="email" name="email" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <input type="Submit" class="submit-button" value="Registre">
    </div>
</form>

我希望有人可以帮助我。

【问题讨论】:

  • 这个脚本包含在文档的什么地方?
  • 我在 之后和 之前都试过了,但都没有用。
  • 如果它是由浏览器预填充的,它就不是空的。什么是预期行为?页面加载时 - 您要重置所有字段吗?
  • 我相信 Chrome 在预填充表单元素时会触发 change 事件。您可以尝试捕获一次以重置值
  • Yuriy Galanter 是对的,我添加了这个 $('.inputWithLabel').change( function(){ var element_id = $(this).attr('id'); $('label[ for="' + element_id + '"]').addClass('inputWithText'); });在 Chrome 中有效。

标签: javascript jquery google-chrome


【解决方案1】:

试试下面的代码:

    $(document).ready(function() {
      $('body .inputWithLabel').each(function(){
        if ($(this).val().length != 0){
          var element_id = $(this).attr('id');
          $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
      });
   });

只需在每个循环中附加正文,即$('body .inputWithLabel')

【讨论】:

    【解决方案2】:

    您的代码看起来不错... 标签没有 .val() .. 如果我们想要标签的内部文本,它应该是 .text() 所以用.text()替换.val(),它会很有希望地工作...... 还要先附加正文,即
    $('body .inputWithLabel')

    【讨论】:

      【解决方案3】:

      试试这个:

      $(document).ready(function() {
      
          $('.inputWithLabel').each(function() {
              if ($(this).val()) {
                  var element_id = $(this).attr('id');
                  $('label[for="' + element_id + '"]').addClass('inputWithText');
              }
          });
      });

      【讨论】:

        【解决方案4】:

        要检查输入是否为空,您可以执行以下更简单的操作。

        $(document).ready(function() {
            $(".inputWithLabel").change(function() {
                if ($(this).val() == null || $(this).val() == "") {
                    alert("Input is null!");
                } else {
                    alert("Input is valid");
                }
            });
        });
        

        【讨论】:

          【解决方案5】:

          你可以在你的网站上试试

          $('.inputWithLabel').each(function() {
              if ($(this).val() !== null && $(this).val() !== "") {
                  var element_id = $(this).attr('id');
                  $("#" + element_id).removeClass('inputWithLabel').addClass('inputWithText');
              }
          });
          

          【讨论】:

            【解决方案6】:

            不要更改您的代码,而是应用此代码:

            $(document).ready(function() {
            
                $('.inputWithLabel').each(function(){
                    if ($(this).val().length != 0 && $(this).val().length != null){
                        var element_id = $(this).attr('id');
                        $('label[for="' + element_id + '"]').addClass('inputWithText');
                    }
                });
            });
            

            仅在$(this).val().length != 0 之后设置&amp;&amp; $(this).val().length != null,它应该在不同的浏览器上运行。试试看!

            【讨论】:

              【解决方案7】:

              我尝试过类似的事情。您所要做的就是首先将您的表单 id 转换为 jQuery 变量,

              var $elem = document.getElementById("yourformid").value; //make sure your id has no # symbol
              if ($elem > 0) {
                  //do stuff
              }
              

              应该可以的。

              【讨论】:

                【解决方案8】:

                目前无法检测表单是否由浏览器自动完成填写。我建议的一些替代方案是:

                禁用自动完成:如果您不需要它,这将是一个简单的解决方案

                <input autocomplete="off">
                

                或整个表格:

                <form autocomplete="off">
                

                使用更改事件来检测字段上的任何更改:

                $('#username').on('change keyup',function(){
                    // to something
                });
                

                如果输入字段有任何更改,我可以在这里设置一个类,例如:

                $('#username').on('change keyup',function(){
                    $(this).addClass('changed');
                });
                

                或者更改提交字段的名称属性。例如:将其用作默认值:

                <input class="inputLabel inputWithLabel" id="username" name="usernameAutofill" type="text">
                

                如果对输入进行了任何操作,稍后将其更改为:

                $('#username').on('change keyup',function(){
                    $(this).attr('name', 'username');
                });
                

                但在您执行任何这些操作之前,请先问自己以下问题: 为什么要证明字段? 如果它们被自动填充会发生什么? 如果它们是空的,会发生什么? 如果他们经常被填满会发生什么?

                因为最好的解决方案在很大程度上取决于您到底想做什么。如果您回答这个问题,我们可以为您提供更多帮助。

                【讨论】:

                  【解决方案9】:

                  尝试使用:

                  $('').value
                  

                  this.value
                  

                  this.textContent
                  

                  $('').innerHTML
                  

                  【讨论】:

                    【解决方案10】:

                    您的代码正在检查每个具有某些值的输入元素,而不是那些为空的元素。 尝试将您的条件更改为:

                    if ($(this).val().length == 0){
                    ...
                    

                    这会将类添加到空字段标签中。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2018-02-05
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-07-13
                      • 2020-07-29
                      • 2012-04-05
                      • 1970-01-01
                      • 2022-07-19
                      相关资源
                      最近更新 更多