帮助使用 javascript 隐藏单选按钮

发布于 2024-09-18 21:06:01 字数 1147 浏览 13 评论 0原文

我最近在这里得到了这段代码,

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

电影里的梦 2024-09-25 21:06:02

如果您不需要 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>

If you do not need jQuery, here is one way of doing it

<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>
机场等船 2024-09-25 21:06:02

您使用的 $() 语法是 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.

痴骨ら 2024-09-25 21:06:02

该代码对我来说工作得很好。

如果它不适合您,请尝试以下操作:

  • 确保 jQuery 正确包含并且正常工作,
  • 将事件处理代码放在这些元素标记之后的任何位置

The code works just fine for me.

If it doesn't work for you, try a couple of things:

  • make sure that jQuery is included properly and is working
  • put your event handling code anywhere after the markup with those elements
無心 2024-09-25 21:06:01

您的代码很好,您只需要用准备好的处理程序包装它即可。

像这样,

$(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');
    });
});​

your code is fine, you just need to wrapped it with the ready handler.

like this,

$(function() {
    $('#discountselection').hide();

    $('#No').click(function() {
        $('#discountselection').hide();
    });

    $('#Yes').click(function() {
        $('#discountselection').show();
    });
});​

you could also shorten your codes like this,

$(function() {
    $('#discountselection').hide();
    $('#Yes, #No').click(function() {
        $('#discountselection').toggle(this.value==='Yes');
    });
});​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文