如何使用Clear Text textarea当我点击单选按钮时

时间:2014-08-26 03:30:14

标签: javascript jquery css

1.Step(检查Unicode RadioButton)
当检查 Unidcode Radio 时,TextetArea notClear 但是 Zawgyi Radio 被检查Textarea是清除

2.Step(Zawgyi RadioButton已检查)
当检查 Zawgyi Radio 时,TextetArea notClear 但是 Unicode Radio 被检查Textarea是清除

<label><input type="radio" name="kb" value="uni" id="unikb"/>Unicode</label>
<label><input type="radio" name="kb" value="zawgyi" id="zawgyikb"/>Zawgyi</label>
<textarea id="desp" class="form-control rows="5"></textarea>

如何使用Jquery

   $("input").click(function(){

      if ( $(this).val()=='uni' || $(this).val()=='zawgyi'){ 
    $("textarea#desp").val();
   }
    else{
    $("textarea#desp").val('');
   }
 }) ;

我想要这种风格

enter image description here
textarea不清楚。

enter image description here
textarea很清楚



enter image description here
textarea不清楚。

enter image description here
textarea很清楚

SEEDEMO

2 个答案:

答案 0 :(得分:1)

我已经更新了你的代码,请看这个JSFiddle

http://jsfiddle.net/hLuk2bhx/22/

代码已更改

 $("textarea#desp").val();  ->   $("#desp").val(''); //You have to assign an "" empty string as its value to clear the textarea content

 esle -> else

添加了html,如下所示

<input type="hidden" id="previouschecked" name="previouschecked" value="" / >

我添加了一个输入[hidden]来存储以前的值,所以它看起来应该是

$("input[type='radio']").click(function(e) {
if ( $(this).val() !=$("#previouschecked").val()){ 
    $("#desp").val('');
}
    $("#previouschecked").val($(this).val());
});

由于您的textarea上有ID,因此您可以直接使用$(&#34;#desp&#34;)

&#34; esle&#34;是一个错字,所以你的代码被打破,不能正确运行

答案 1 :(得分:1)

你有其他错误,jsfiddle修正:http://jsfiddle.net/hLuk2bhx/9/ 现在它正在运作......(:

$("input[type='radio']").click(function(e) {
    if ( $(this).val()=='uni'){ 
        $("textarea").val('');
    }else{
        $("textarea").val('');
    }    
});

在您发表评论后我已经更新了您的代码,使其更具动态性,我创建了一个var来控制最后一个语句。

var lastChecked = undefined;
$("input[type='radio']").click(function(e) {
    if ( $(this).val() != lastChecked && (typeof lastChecked !== 'undefined')){ 
        $("textarea#desp").val('');
    }
    lastChecked = $(this).val();
});

希望它能帮到你解决问题。 小提琴:http://jsfiddle.net/hLuk2bhx/23/