2013-03-23 42 views
-1

我只是想知道在複選框按鈕被選中時,將輸入類型從隱藏改爲文本的最佳方式是什麼。當複選框被選中時,將輸入類型隱藏爲文本

我期待這樣的事情

<div class="options"> 
    <input type="checkbox" checked=""> 
    </div> 

    <form> 
     <input type="hidden"> 
    </form> 

,然後一個複選框被選中

<div class="options"> 
    <input type="checkbox" checked="checked"> 
    </div> 

    <form> 
     <input type="text"> 
    </form> 
+0

http://www.w3schools.com/jquery/jquery_dom_set.asp 檢查出的「設置屬性」 – 2013-03-23 23:49:54

+0

請張貼在jsFiddle.net – j08691 2013-03-23 23:50:23

+0

你已經嘗試了代碼,並儘可能的例子的例子中,沒有problemo! :) – 2013-03-23 23:50:56

回答

2

我就是這麼做的,爲了好玩:

$(document).ready(function() { 
    $("#txt").toggle(); 
    $("#chk").change(function() { 
     $("#txt").toggle(); 
    }); 
}); 

<input type="checkbox" id="chk" /> 
<input type="text" id="txt" /> 

而不是從隱藏的文本改變它的,我只是切換的知名度單個輸入元素。這當然是隱藏起來的,並且可以看到你的勾選,如果你不勾選,就看不見。如果你想要不同的行爲,你會做一些小的工作,但我相信你足夠聰明來處理它。

祝您好運!

+0

感謝邁克,我明白了! – user2201765 2013-03-24 00:22:15

0

退房this live jsfiddle

HTML:

<input type="checkbox" class="check1">Show checkbox</input> 
<input type="hidden" class="hidden1" value="Some Value" /> 
<input type="text" class="text1" style="display:none" /> 

JQUER Y:

$(".check1").click(function() { 
    if ($('.check1').is(':checked')) { 
     $(".text1").val($(".hidden1").val()); 
     $(".text1").show(); 
    } else { 
     $(".text1").val(""); 
     $(".text1").hide(); 
    } 
}); 



根據您發佈的代碼,這是你在找什麼:

$('.options input[type='checkbox']').click(function() { 
    if ($('.options input[type='checkbox']').is(':checked')) { 
     $('form').html("<input type='text'>"); 
    } else { 
     $('form').html("<input type='hidden'>"); 
    } 
}); 

爲什麼你要做到這一點,我沒有用,雖然。你只是想顯示和隱藏文本框?在這種情況下,你可以這樣寫:

$('.options input[type='checkbox']').click(function() { 
    $('input[type='text']').toggle(); 
}); 
相關問題