9

我有一个 JavaScript 来检查一个字段是否等于 15 个字符,如果不是,则提交按钮是灰色的。如果我手动输入 15 个字符,这将非常有效,但如果我粘贴 15 个字符,则不会。即使将内容粘贴到字段中,我如何才能检查?

我可以让它定期(秒)检查字符还是有“检查粘贴”的功能?

我的脚本:

<script type="text/javascript">
jQuery(document).ready(function($){
$('input[type=submit]').attr("disabled","disabled");
$('input[name="item_meta[1234]"]').keypress(function() { //change # to the ID of your field
        if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected
              $('input[type=submit]').attr("disabled","disabled");
        } else { //enable the submit button
              $('input[type=submit]').removeAttr('disabled');
        }
  });
});
</script>

<form>
<div id="frm_field_1234_container" class="frm_form_field form-field  frm_required_field frm_top_container">
<label class="frm_primary_label">Minimun 15 char:
    <span class="frm_required"></span>
</label>
<input type="text" id="field_ygtw9u" name="item_meta[1234]" value=""  maxlength="15" class="required"/>

<p class="submit">
<input type="submit" value="Submit" />
</p>
</form>
4

4 回答 4

19

更改keypresskeyup它应该可以工作。

于 2013-03-11T08:31:31.957 回答
1

尝试类似的东西

var $submit = $('input[type=submit]').attr("disabled","disabled");
$('input[name="item_meta[1234]"]').keyup(function() { //change # to the ID of your field
    var $this = $(this);
    if ($this.val().length < 14) { //disable submit if "no" is selected
        $submit.attr("disabled","disabled");
    } else { //enable the submit button
        $submit.removeAttr('disabled');
    }
  });

$('#field_ygtw9u').on('paste', function(e){
    var $this = $(this);
    setTimeout(function() {
        if ($this.val().length < 14) { //disable submit if "no" is selected
            $submit.attr("disabled","disabled");
        } else { //enable the submit button
            $submit.removeAttr('disabled');
        }
    }, 0);
});

演示:小提琴

不要使用事件,因为在 IE/Chrome 中keypress按下时不会触发它backspace

于 2013-03-11T08:35:31.580 回答
1

测试,工作。小提琴:http: //jsfiddle.net/fnkr/MXnuk/

<script type="text/javascript">
    function checkInput() {
        if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected
                $('input[type=submit]').attr("disabled", "disabled");
            } else { //enable the submit button
                $('input[type=submit]').removeAttr('disabled');
            }
    }

    jQuery(document).ready(function ($) {
        $('input[type=submit]').attr("disabled", "disabled");
        $('input[name="item_meta[1234]"]').bind("input", function () { //change # to the ID of your field
            checkInput();    
        });

        $('input[name="item_meta[1234]"]').bind("propertychange", function () { //change # to the ID of your field
            checkInput();
        });
    });
</script>

<form>
    <div id="frm_field_1234_container" class="frm_form_field form-field  frm_required_field frm_top_container">
        <label class="frm_primary_label">Minimun 15 char: <span class="frm_required"></span>

        </label>
        <input type="text" id="field_ygtw9u" name="item_meta[1234]" value="" maxlength="15"
        class="required" />
        <p class="submit">
            <input type="submit" value="Submit" />
        </p>
</form>
于 2013-03-11T08:43:10.993 回答
0

您应该使用propertychange(IE)和input事件。现场演示

$('input[name="item_meta[1234]"]').bind("input", function() { //change # to the ID of your field
    if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected
          $('input[type=submit]').attr("disabled","disabled");
    } else { //enable the submit button
          $('input[type=submit]').removeAttr('disabled');
    }
});

$('input[name="item_meta[1234]"]').bind("propertychange", function() { //change # to the ID of your field
    if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected
          $('input[type=submit]').attr("disabled","disabled");
    } else { //enable the submit button
          $('input[type=submit]').removeAttr('disabled');
    }
});
于 2013-03-11T08:31:24.533 回答