簡體   English   中英

根據單選按鈕的選擇顯示/隱藏文本文件

[英]Show/hide textfile depending on radio buttons' selection

我知道這是一個重復的問題,但是我無法使其運行。 我嘗試了在SO和谷歌搜索中找到的所有解決方案...

我的頁面上有此單選按鈕表單。 我想要的是,當用戶選擇“ 僅一個與會者 ”選項時,此選項僅顯示一個文本字段。 如果用戶選擇另一個,它將消失。

正常形式

用戶選擇一個選項后,將顯示文本框。

擴展形式

我一直在嘗試使用javascript。 我用這段代碼

<script>
    $(document).ready(function()
        $("#send_to_one").hide();
        $("input:radio[name="people"]").change(function(){  
          if(this.checked){
            if(this.value =='one'){
              $("#send_to_one").show()
            }else{
              $("#send_to_one").hide();
            }
         }
    }
  });
</script>

表單的代碼是

  <div id="send_to">
    <input type="radio" id="send_poll" name="people" value="all" checked="checked">all the attendees</br>      
    <input type="radio" id="send_poll" name="people" value="one" >only one attendee<br/>
    <div id="send_to_one">
      <label>Write the attendee's name: </label><input type="text" id="attendeename"><br/><br/>
    </div>
    <input type="radio" id="send_poll" name="people" value="group">a group of attendees</br>              
  </div>    

我已經檢查了JavaScript文件是否已加載。 我還嘗試將javascript代碼放在表單所在的html.erb文件中,單獨的.js文件和application.htm.erb的<head></head>部分中,但是沒有運氣。 為了正常工作,我需要在哪里准確放置每個代碼部分?

使用Rails 3.0.4,Ruby 1.8.9。 我也在使用JQuery

現場演示

HTML:

<div id="send_to">
  <input type="radio" id="send_poll" name="people" value="all" checked="checked" />all the attendees<br/>      
  <input type="radio" id="send_poll" name="people" value="one" />only one attendee<br/>
  <div id="send_to_one">
      <label>Write the attendee's name: </label><input type="text" id="attendeename" /><br/><br/>
  </div>
  <input type="radio" id="send_poll" name="people" value="group" />a group of attendees<br/>              
</div>

jQ:

$(document).ready(function(){

    $("#send_to_one").hide();

    $("input:radio[name='people']").change(function(){  

            if(this.value == 'one' && this.checked){
              $("#send_to_one").show();
            }else{
              $("#send_to_one").hide();
            }

    });

});
$(document).ready(function() {
    $("#send_to_one").hide();
    $("input:radio[name='people']").change(function(){  
         if(this.checked){
            if(this.value =='one'){
              $("#send_to_one").show()
            }else{
              $("#send_to_one").hide();
            }
         } 
    });
});

您的代碼是正確的,但是缺少一些花括號,方括號和未轉義的引號。 您正在使用任何形式的智能javascript編輯器嗎? 因為你真的應該...

http://jsfiddle.net/7yt2A/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM