gpt4 book ai didi

javascript - 禁用 html 表单提交按钮,以启用勾选的复选框

转载 作者:行者123 更新时间:2023-11-30 12:49:37 24 4
gpt4 key购买 nike

我试图禁用提交按钮,并在勾选复选框时启用它。简单示例适用于 jsfiddle

http://jsfiddle.net/8YBu5/522/

但在我的模板(使用 Bootstrap 等)中尝试时不是这样。

我的模板有复选框

<input type="checkbox"  id="checky"><a href="#">terms and conditions</a>

然后是按钮

<input type="submit" value="Submit" id="submit" class="btn btn-success btn-lg btn-block">Submit</button>

然后是javascript。 javascript 是否需要位于顶部或其他位置?

<script type="text/javascript">
$('#submit').attr("disabled","disabled");

$('#checky').click(function(){

if($(this).attr('checked') == true){
$('#submit').removeAttr('disabled');
}
else {
$('#submit').attr("disabled","disabled");

}
});
</script>

有人看到我哪里出错了吗?

谢谢

更新感谢所有建议,我现在尝试将 $(document).ready(function() { 添加到脚本的开头并将 .attr 更改为 .prop ,仍然无法正常工作。更多信息:按钮从一开始就没有被禁用,所以也许我没有在函数中正确引用按钮?

更新2我已尝试添加警报以查看我的 javascript 是否正在运行,但警报未显示,这意味着什么?

 <script type="text/javascript">
$(document).ready(function() {
alert("Welcome");
$('submit').prop("disabled", true);
$('checky').click(function(){
if($(this).attr('checked') == true){
$('submit').prop("disabled", false);
}
else {
$('submit').prop("disabled", true);
}
});
});

UPDATE3 javascript 现在正在运行,我添加了两个警报,以查看正在执行的内容。

//<![CDATA[ 
$(window).load(function(){
$('#postme').attr("disabled","disabled");

$('#checky').click(function(){

if($(this).attr('checked') == true){
alert( "w" );
$('#postme').removeAttr("disabled");
}
else {
alert( "e" );
$('#postme').attr("disabled","disabled");

}
});
});//]]>

当我点击打开或关闭复选框时,我收到 e 警报,告诉我 if 语句始终为假。有什么想法吗?

更新4该代码段有效,只是不在我的页面中,始终执行警报 2,从不执行警报 1 标题

<link href="/static/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">


<div class="row padding">
<div class="col-md-12">
<h1></h1>
</div>
</div>

<div class="row padding">

<form id="new_form" action="/page/" method="POST" >

<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"></h3>
</div>

<div class="panel-body">
<div class="col-md-12">

</div>
</div>
</div>

<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Creator submission form</h3>
</div>

<div class="panel-body">
<div class="col-md-12">

</div>
</div>
</div>
<br>

<input type="checkbox" id="checky1">
<input type="submit" id="postme1" value="submit">

</form>
</div>

<div class="row padding">
<div class="col-md-12">

</div>
</div>
</div>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('#postme1').attr("disabled","disabled");

$('#checky1').click(function(){

if($(this).attr('checked') == true){
$('#postme1').removeAttr('disabled');
}
else {
$('#postme1').attr("disabled","disabled");
}
});
});//]]>

</script>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/static/js/bootstrap.min.js"></script>

<div class="container">
<footer>
<hr>

</footer>
</div>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('#postme1').attr("disabled","disabled");

$('#checky1').click(function(){

if($(this).attr('checked') == true){
alert("1");
$('#postme1').removeAttr('disabled');
}
else {
alert("2");
$('#postme1').attr("disabled","disabled");

}
});
});//]]>

</script>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/static/js/bootstrap.min.js"></script>

</body>
</html>

最佳答案

你的javascript代码应该是

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('#postme').attr("disabled","disabled");

$('#checky').click(function(){

if($(this).attr('checked') == true){
$('#postme').removeAttr('disabled');
}
else {
$('#postme').attr("disabled","disabled");

}
});
});//]]>

</script>


</head>
<body>
<input type="checkbox" id="checky"><a href="#">terms and conditions</a>
<input type="submit" id="postme" value="submit">

</body>


</html>

这段代码对我来说工作正常。

更新

将其用作复选框的点击事件处理程序

$('#checky').click(function(){

if($(this).prop('checked')){
$('#postme').removeAttr('disabled');
}
else {
$('#postme').attr("disabled","disabled");

}
});

关于javascript - 禁用 html 表单提交按钮,以启用勾选的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21408505/

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