如果单击选项No,则删除输入值

时间:2014-04-11 18:02:47

标签: javascript jquery

如果用户单击“是”并在输入框中输入值但是改变主意并单击“否”选项但是输入值仍然存在,我该如何删除该值

<div class="control-group">
    <label class="control-label" for="radios">Do you offer rented call center seating on a monthly basis?</label>
    <div class="controls">
        <label class="radio" for="yes">
            <input type="radio" name="radios-1" id="radios-1" value="Yes">Yes</label>
        <label class="radio" for="no">
            <input type="radio" name="radios-1" id="radios-1" value="No">No</label>
    </div>
</div>
<div class="form-group" style="display:none;" id="seatnumber">
    <label class="control-label" for="name">How many seats do you have available?</label>
    <div class="input-group">   <span class="input-group-addon"><i class="fa fa-user"></i></span>

        <input type="text" class="form-control  input-normal" placeholder="Enter number of seats here..." name="monthly_rental" id="monthly_rental">
    </div>
</div>

JS

        $('input[name|="radios-1"]').change(function() {

        if($(this).val()=='Yes') {
            $('#seatnumber').fadeIn();

        } else {

            $('#seatnumber').fadeOut();
        }
    });

2 个答案:

答案 0 :(得分:2)

试试这个:

$('input[name|="radios-1"]').change(function() {
        if($(this).val()=='Yes') {
            $('#seatnumber').fadeIn();
        } else {
            $('#seatnumber input').val(''); // remove value when user click No
            $('#seatnumber').fadeOut();
        }
});

<强> DEMO

答案 1 :(得分:0)

   $('input[name|="radios-1"]').change(function() {
        $('#monthly_rental').val('');
        if($(this).val()=='Yes') {
            $('#seatnumber').fadeIn();

        } else {

            $('#seatnumber').fadeOut();
        }
    });

演示:

http://jsfiddle.net/7Te9d/