0

我有一个 HTML 订阅表单,它使用(或应该使用)一个 jquery 代码来灰显提交按钮,直到字段名称被填写。

但是,它不起作用...

有什么问题的线索吗?这是代码:

<head>
<script type="text/javascript">
var $submit = $("input[type=submit]");
if ( $(".subName").length > 0 ) {
   $submit.attr("disabled","disabled");
} else {
   $submit.removeAttr("disabled");
}
</script>
</head>
<body>
<form action="" method="post" id="subscribe" name="subscribe">
<input type="hidden" name="action" value="subscribe">
<label>NAME:</label>
<input type="text" name="name" class="subName" id="sub_first_name">
<label>EMAIL:</label>
<input type="text" name="email" class="subEmail" id="sub_email">
<input type="submit" name="button" value="Subscribe" />
</form>
</body>

谢谢!

- - - - - - - - - - - - - 编辑

<form action="" method="post" id="subscribe" name="subscribe">
<label>NAME:</label>
<input type="text" name="name" class="subName" id="sub_first_name">
<label>EMAIL:</label>
<input type="text" name="email" class="subEmail" id="sub_email">
<input type="submit" name="button" value="Subscribe" />
</form>

JS:

<script type="text/javascript">
$(document).ready(function() {
    var $submit = $("input[type=submit]");
    $('.subName').on('change', function() {
        if ( ! $(".subName").val().length ) {
           $submit.attr("disabled","disabled");
        } else {
           $submit.removeAttr("disabled");
        }
    });
}
</script>

但仍然无法正常工作:(

4

2 回答 2

0
$(document).ready(function() {
    var $submit = $("input[type=submit]");
    $('.subName').on('change keyup', function() {
        if ( !$(this).val().length ) {
           $submit.attr("disabled","disabled");
        } else {
           $submit.removeAttr("disabled");
        }
    });
});
于 2013-01-31T14:32:14.277 回答
0

除了将其绑定到事件之外,您是否还在标题中包含 jQuery?例如像这样:

<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
于 2013-01-31T14:39:16.810 回答