帮助使用 javascript 隐藏单选按钮
我最近在这里得到了这段代码,
<script type='text/javascript'>
$('#discountselection').hide();
$('#No').click(function(){
$('#discountselection').hide();
});
$('#Yes').click(function(){
$('#discountselection').show();
});
</script>
目的是根据是否选择单选按钮“是”和“否”来隐藏下拉框。这是我的代码:
<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一点也不熟悉。如果有人能看到我哪里出错了,这将是一个很大的帮助。 谢谢。
I got this code on here recently
<script type='text/javascript'>
$('#discountselection').hide();
$('#No').click(function(){
$('#discountselection').hide();
});
$('#Yes').click(function(){
$('#discountselection').show();
});
</script>
The aim being to hide a drop down box depending on whether the radio buttons yes and no were selected. Here is my code:
<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>
I have the javascript placed in between the head tags, but for some reason this just isn't working for me. I'm not familiar with javascript at all. If any one could see where I am going wrong this would be a big help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不需要 jQuery,这是一种方法
If you do not need jQuery, here is one way of doing it
您使用的
$()
语法是 jQuery 语法,而不是普通的 Javascript。 JQuery 是一个 Javascript 库,它在普通 JS 的基础上添加了附加功能。如果您使用此语法,则需要包含 JQuery 库。即使没有 JQuery,您想要实现的目标也相对简单,但如果您不打算使用 JQuery,代码看起来会完全不同。
如果您计划使用 JQuery,则需要将代码包装在文档就绪块中,以便正确初始化。
The
$()
syntax that you're using is jQuery syntax, not plain Javascript. JQuery is a Javascript library which adds addtional functionality over normal JS. If you're using this syntax, you'll need to include the JQuery library.What you're trying to achieve is relatively simple, even without JQuery, but the code will look completely different if you don't plan to use JQuery.
If you are planning to use JQuery, your code needs to be wrapped in a docuemnt ready block, so that it gets initialised correctly.
该代码对我来说工作得很好。
如果它不适合您,请尝试以下操作:
The code works just fine for me.
If it doesn't work for you, try a couple of things:
您的代码很好,您只需要用准备好的处理程序包装它即可。
像这样,
你也可以这样缩短你的代码,
your code is fine, you just need to wrapped it with the ready handler.
like this,
you could also shorten your codes like this,