开发者

Disable and enable textbox on check click

开发者 https://www.devze.com 2023-02-15 19:47 出处:网络
If checkbox is checked than textbox should be enabled otherwise textbox should be disabled. Don\'t know why this code is not working ?

If checkbox is checked than textbox should be enabled otherwise textbox should be disabled.

Don't know why this code is not working ?

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

Any Idea ?


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" />


Try setting disabled = true and disabled=false..also id of the text_box will be test_from_address and name will be test[from_address].


Try setting disabled=disabled at the end of line 3


This worked for me ..

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


$(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");
            }
        });
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号