javascript - 在选中单击时禁用和启用文本框

标签 javascript html ruby-on-rails

如果选中复选框,则应启用文本框,否则应禁用文本框。

不知道为什么此代码不起作用?

<script type="text/javascript">
function toggleTB(what){
if(what.checked){document.test.from_address.disabled=1}
else{document.test.from_address.disabled=0}}
</script>

<% form_for(@test,:name => "test") do |f| %>
<table>
        <tr>
            <td class="right upcase none">
              <%= f.label 'text_enabled', "Enable",:class => "capitalize none" %>
              <%= f.check_box :text_enabled,:onclick => "toggleTB(this)" %>
            </td>
        </tr>

        <tr>
            <td class="right upcase none">
              <%= f.label 'from_address', "From Address",:class => "capitalize none" %>
            </td>
            <td>
              <%= f.text_field 'from_address', :maxlength => 16 %>
            </td>
        </tr>
</table>
<% end %>

有什么想法吗?

最佳答案

Try like this

<script type="text/javascript">
   function toggleTB(what){
      if(what.checked){document.getElementById('from_address').disabled=1}
   else{document.getElementById('from_address').disabled=0}}
</script>

<input type="checkbox" name="toggle" onclick="toggleTB(this)"/>
<input type="input" id="from_address" />

关于javascript - 在选中单击时禁用和启用文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5258791/

相关文章:

css - 如何在 haml 中正确显示 Font Awesome 图标

javascript - 属于数组中同一字段的值的累积和

javascript - 如何等待 Canvas 上的点击

html - 如何根据字体大小在 HTML 中设置输入标签的高度

javascript - 在java中使用jsoup保存网页src和链接

html - 在 Safari 浏览器中下载 CSV

ruby-on-rails - ruby rails : Validate CSV file

javascript - 如何创建和使用动态分层 JSON?

javascript - 全日历 : trouble rendering events with ajax when pressing 'prev' or 'next'

ruby-on-rails - 使用 devise + omniauth + rails 3 时如何清除 facebook session 和 cookie?