gpt4 book ai didi

javascript - 单击单选按钮时输入不会消失

转载 作者:技术小花猫 更新时间:2023-10-29 12:05:44 25 4
gpt4 key购买 nike

我正在尝试编写一个 html css js 代码。在它的一部分中,我有一组单选按钮(包括 2 个元素)。我希望发生这种情况:如果我点击第一个单选按钮,textbox1 出现,如果我点击第二个单选按钮,textbox1 消失,textbox2 出现,反之亦然。

当我点击其中一个时会发生这种情况,但当我点击第二个时它不起作用。

这是我的html代码:

<label>Which one do you want to enter?</label>
<br/>
<label>Budget:</label>
<input name = "submethod" id = "submethodbudget" type="radio" value = "bud"/>
<div id = "smethod" style="display:none">
<input type="text" name = "budgetsub">
</div>
<label>Number of Clicks per Month:</label>
<input name = "submethod" id= "submethodclicks" type="radio" value = "clckno"/>
<div id = "smethod2" style="display:none">
<input type="text" name = "clicksnosub">
</div>
<br/>

这是我的 js:

<script type="text/javascript">
$("#submethodbudget").click(function() {
$("#smethod").show("300");
$('#smethod').css('display', ($(this).val() === 'bud') ? 'block':'none');
});
$("#submethodbudget").click(function() {
$("#smethod2").hide("300");
});
</script>

<script type="text/javascript">
$("#submethodclicks").click(function() {
$("#smethod2").show("300");
$('#smethod2').css('display', ($(this).val() === 'clckno') ? 'block':'none');
});
$("#submethodclicks").click(function() {
$("smethod").hide("300");
});
</script>

你能告诉我我做错了什么吗?

最佳答案

改变

$("smethod").hide("300"); 

到:

$("#smethod").hide("300");  

我编辑你的代码:

<html>
<head>
</head>
<body>
<label>Which one do you want to enter?</label>
<br/>
<label>Budget:</label><input name = "submethod" id = "submethodbudget" type="radio" value = "bud"/>
<div id = "smethod" style="display:none">
<input type="text" name = "budgetsub">
</div>
<label>Number of Clicks per Month:</label><input name = "submethod" id= "submethodclicks" type="radio" value = "clckno"/>
<div id = "smethod2" style="display:none">
<input type="text" name = "clicksnosub">
</div>
<br>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$("#submethodbudget").click(function() {
$("#smethod").show("300");
$('#smethod').css('display', ($(this).val() === 'bud') ? 'block':'none');
$("#smethod2").hide("300");
});

$("#submethodclicks").click(function() {
$("#smethod2").show("300");
$('#smethod2').css('display', ($(this).val() === 'clckno') ? 'block':'none');
$("#smethod").hide("300");
});
</script>
</body>
</html>

关于javascript - 单击单选按钮时输入不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029014/

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