Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击复选框时禁用并启用文本框_Javascript_Html_Ruby On Rails - Fatal编程技术网

Javascript 单击复选框时禁用并启用文本框

Javascript 单击复选框时禁用并启用文本框,javascript,html,ruby-on-rails,Javascript,Html,Ruby On Rails,如果选中复选框,则应启用textbox,否则应禁用textbox 不知道为什么这个代码不起作用 <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,

如果选中复选框,则应启用textbox,否则应禁用textbox

不知道为什么这个代码不起作用

<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 %>

功能切换TB(什么){
如果(what.checked){document.test.from_address.disabled=1}
else{document.test.from_address.disabled=0}
“测试”)do | f |%>
“无大写”%>
“切换TB(此)”%>
“无大写”%>
16 %>

有什么想法吗?

试着在第3行末尾设置
disabled=disabled

试着这样做
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" />
功能切换TB(什么){ if(what.checked){document.getElementById('from_address').disabled=1} else{document.getElementById('from_address').disabled=0}
这对我很有效

<script type="text/javascript">
  <!--
function toggleTB(what){
 document.getElementById("test_from_address").disabled = !what.checked;
}
-->
</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 %>

“测试”)do | f |%>
“无大写”%>
“切换TB(此)”%>
“无大写”%>
16 %>

尝试设置
disabled=true
disabled=false
。文本框的id将是
test\u from\u address
,名称将是
test[from\u address]

这是如何工作的?…我的想法是,文本字段的id将是
test\u from\u address
,就像Rails在表单中那样…所以我认为您必须执行
document.getElementById(“test\u from\u address”)。disabled=!检查什么
$(function()
    {
        $("#dropdown").change(function()
        {
            if ($(this).val()== "option1")
            {
                $("#textbox1").removeAttr("disabled");
                $("#textbox2").removeAttr("disabled");
            }
            else 
            {
                $("#textbox2").attr("disabled", "disabled");
                $("#textbox2").attr("disabled", "disabled");
            }
        });
        $("#PaymentMode").change(function()
        {
            if ($(this).val()== "option2")
            {
                $("#textbox3").removeAttr("disabled");
                $("#textbox4").removeAttr("disabled");
                $("#textbox5").removeAttr("disabled");
            }
            else
            {
                $("#textbox3").attr("disabled", "disabled");
                $("#textbox4").attr("disabled", "disabled");
                $("#textbox5").attr("disabled", "disabled");
            }
        });
    });