javascript帮助隐藏单选按钮
我最近在这里得到了这个密码javascript帮助隐藏单选按钮,javascript,jquery,drop-down-menu,radio-button,Javascript,Jquery,Drop Down Menu,Radio Button,我最近在这里得到了这个密码 <script type='text/javascript'> $('#discountselection').hide(); $('#No').click(function(){ $('#discountselection').hide(); }); $('#Yes').click(function(){ $('#discountselection').show(); }); <
<script type='text/javascript'>
$('#discountselection').hide();
$('#No').click(function(){
$('#discountselection').hide();
});
$('#Yes').click(function(){
$('#discountselection').show();
});
</script>
$(“#折扣选择”).hide();
$('#否')。单击(函数(){
$(“#折扣选择”).hide();
});
$(“#是”)。单击(函数(){
$(“#折扣选择”).show();
});
其目的是根据是否选择了单选按钮yes和no来隐藏下拉框。这是我的密码:
<td width="338">Would You like to avail of our multiyear discounts?*
<br />See <a href="Pricing">Pricing</a> for more details
</td>
<td colspan="4">
<input name="discount" type="radio" id="Yes" value="Yes" />Yes
<input name="discount" type="radio" id="No" value="No" checked="checked" />No<br />
<select class="purple" name="discountselection" id="discountselection">
<option value="1" selected="selected">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
</select>
</td>
您想享受我们的多年折扣吗*
有关详细信息,请参阅
对
否
一年
两年
三年
我将javascript放在head标记之间,但出于某种原因,这对我来说不起作用。我对javascript一点也不熟悉。如果有人能看出我哪里出了问题,这将是一个很大的帮助。
谢谢。代码对我来说很好用 如果它不适合您,请尝试以下几种方法:
- 确保jQuery被正确地包含并正常工作
- 将事件处理代码放在带有这些元素的标记之后的任意位置
- 确保jQuery被正确地包含并正常工作
- 将事件处理代码放在带有这些元素的标记之后的任意位置
$(function() {
$('#discountselection').hide();
$('#No').click(function() {
$('#discountselection').hide();
});
$('#Yes').click(function() {
$('#discountselection').show();
});
});
$(function() {
$('#discountselection').hide();
$('#Yes, #No').click(function() {
$('#discountselection').toggle(this.value==='Yes');
});
});
你也可以像这样缩短代码
$(function() {
$('#discountselection').hide();
$('#No').click(function() {
$('#discountselection').hide();
});
$('#Yes').click(function() {
$('#discountselection').show();
});
});
$(function() {
$('#discountselection').hide();
$('#Yes, #No').click(function() {
$('#discountselection').toggle(this.value==='Yes');
});
});
您的代码很好,只需要用ready处理程序包装它 像这样,
$(function() {
$('#discountselection').hide();
$('#No').click(function() {
$('#discountselection').hide();
});
$('#Yes').click(function() {
$('#discountselection').show();
});
});
$(function() {
$('#discountselection').hide();
$('#Yes, #No').click(function() {
$('#discountselection').toggle(this.value==='Yes');
});
});
你也可以像这样缩短代码
$(function() {
$('#discountselection').hide();
$('#No').click(function() {
$('#discountselection').hide();
});
$('#Yes').click(function() {
$('#discountselection').show();
});
});
$(function() {
$('#discountselection').hide();
$('#Yes, #No').click(function() {
$('#discountselection').toggle(this.value==='Yes');
});
});
如果您不需要jQuery,这里有一种方法
<script type="text/javascript">
function showDiscount(show) {
document.getElementById('discountselection').style.display=(show)?'block':'none';
}
window.onload=function() {
showDiscount(document.getElementById('discountYes').checked);
}
</script>
<table>
<tr>
<td width="338">Would You like to avail of our multiyear discounts?*
<br />See <a href="Pricing">Pricing</a> for more details
</td>
<td colspan="4">
<input name="discount" type="radio" id="discountYes" value="Yes" onclick="showDiscount(this.checked)"/>Yes
<input name="discount" type="radio" id="discountNo" value="No" checked="checked" onclick="showDiscount(!this.checked)"/>No<br />
<select class="purple" name="discountselection" id="discountselection" style="display:none">
<option value="1" selected="selected">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
</select>
</td>
</tr>
</table>
功能展示折扣(show){
document.getElementById('discountselection').style.display=(show)?'block':'none';
}
window.onload=function(){
showDiscount(document.getElementById(“折扣”)。选中);
}
您愿意享受我们多年的折扣吗*
有关详细信息,请参阅
对
否
一年
两年
三年
如果您不需要jQuery,这里有一种方法
<script type="text/javascript">
function showDiscount(show) {
document.getElementById('discountselection').style.display=(show)?'block':'none';
}
window.onload=function() {
showDiscount(document.getElementById('discountYes').checked);
}
</script>
<table>
<tr>
<td width="338">Would You like to avail of our multiyear discounts?*
<br />See <a href="Pricing">Pricing</a> for more details
</td>
<td colspan="4">
<input name="discount" type="radio" id="discountYes" value="Yes" onclick="showDiscount(this.checked)"/>Yes
<input name="discount" type="radio" id="discountNo" value="No" checked="checked" onclick="showDiscount(!this.checked)"/>No<br />
<select class="purple" name="discountselection" id="discountselection" style="display:none">
<option value="1" selected="selected">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
</select>
</td>
</tr>
</table>
功能展示折扣(show){
document.getElementById('discountselection').style.display=(show)?'block':'none';
}
window.onload=function(){
showDiscount(document.getElementById(“折扣”)。选中);
}
您愿意享受我们多年的折扣吗*
有关详细信息,请参阅
对
否
一年
两年
三年
您使用的$()
语法是jQuery语法,而不是普通的Javascript。JQuery是一个Javascript库,它在普通JS上添加了附加功能。如果使用这种语法,则需要包含JQuery库
即使没有JQuery,您试图实现的也是相对简单的,但是如果您不打算使用JQuery,代码看起来会完全不同
如果您计划使用JQuery,您的代码需要包装在docuemnt ready块中,以便正确初始化。您使用的$()
语法是JQuery语法,而不是普通Javascript。JQuery是一个Javascript库,它在普通JS上添加了附加功能。如果使用这种语法,则需要包含JQuery库
即使没有JQuery,您试图实现的也是相对简单的,但是如果您不打算使用JQuery,代码看起来会完全不同
如果您计划使用JQuery,则需要将代码包装在docuemnt ready块中,以便正确初始化它。是否包含JQuery?这个脚本似乎在使用它。“在标签之间”是什么意思?什么标签?@halfdan..我以为我的问题解决了,因为我忘了链接到jquery文件,但后来我做了,但它仍然不起作用@安娜卡塔-对不起,我指的是标题标签,由于某些原因,标题没有显示。您是否包括jQuery?这个脚本似乎在使用它。“在标签之间”是什么意思?什么标签?@halfdan..我以为我的问题解决了,因为我忘了链接到jquery文件,但后来我做了,但它仍然不起作用@安娜卡塔-对不起,我指的是标题标签,因为某些原因,标题没有显示。嗨,德兰,我想你完全复制了我的代码?我只是不明白会是什么。这段代码甚至可以在JSFIDLE上运行,但对我来说似乎不起作用。。。您可能希望在任何否决投票发生之前删除此项;)完成后,我添加了一些确定的提示。嗨,德兰,我想你完全复制了我的代码?我只是不明白会是什么。这段代码甚至可以在JSFIDLE上运行,但对我来说似乎不起作用。。。您可能希望在任何否决投票发生之前删除此项;)完成后,我添加了一些确定的提示。谢谢,简单但重要的提示!谢谢你,简单但重要的事情!