gpt4 book ai didi

javascript - 我的 Jquery 按钮 - 禁用/启用功能不起作用。谁能告诉我怎么了?

转载 作者:行者123 更新时间:2023-11-30 17:08:20 28 4
gpt4 key购买 nike

我试图禁用提交按钮,直到前两个字段有输入。我已经编写了一个函数来执行此操作,但由于某种原因它无法正常工作。这是我的 HTML:

<html>
<Head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="Stuff.js"></script>
</Head>
<body>
<div class="container">
<form role="form">
<div class="form-group has-error">
<label for="FirstName">First Name</label>
<input type="text" class="form-control" id="First" placeholder="First Name">
</div>
<div class="form-group">
<label for="LastName">Last Name</label>
<input type="text" class="form-control" id="Last" placeholder="Last Name">
</div>
<div class="form-group">
<label for="Age">Age</label>
<input type="text" class="form-control" id="Age" placeholder="Age">
</div>
<button type="submit" class="btn btn-default" id="submit" >Submit</button>
</form>
</div>
</body>

这是我的功能:

$('#submit').keyup(function () {
if ($('#First').val() != "" && $('#Last').val() != "") {
$('#submit').removeattr('disabled');
} else {
$('#submit').attr('disabled', true);
}

});

最佳答案

你可以使用这样的东西:

$('#First, #Last').keyup(function () {
if ($('#First').val() !== "" && $('#Last').val() !== "") {
$('#submit').prop('disabled', false);
} else {
$('#submit').prop('disabled', true);
}
});

请注意,您需要在键入时检查字段本身。

此外,使用 prop() 将禁用属性的基础 bool 属性设置为 true/false,而不是完全删除它 - see here for why .

jsFiddle here.

关于javascript - 我的 Jquery 按钮 - 禁用/启用功能不起作用。谁能告诉我怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com