display data in a textbox when checkbox is checked

前端 未结 2 1192
谎友^
谎友^ 2020-12-21 15:31

I have three text boxes having id text1, text2, text3 and one checkbox having id check. I am displaying something in first textbox let\'s say 10. Now if i will check checkbo

2条回答
  •  醉酒成梦
    2020-12-21 15:57

    Your question is not easy to understand....

    When checkbox is checked the next empty input text have to display '20' value ? And if checkbox is unchecked last input with '20' is cleared value ?

    Add the "checker" class to each input text.

    
    
    
    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    Use this jquery script instead of yours

    $(document).ready(function(){
        $('#check').click(function(){
    
            if($(this).is(':checked'))
            {
                var stop='';
                $('.checker').each(function(){
                    if($(this).val()=='' && !stop)
                    {
                       $(this).val('20');
                        stop='stop'; 
                       // Add something like that to set other textbox values
                       var add = parseInt($('#anotherTB').val()) + parseInt($(this).val());
                       $('#anotherTB').val(add); 
                    }
                });
            }
    
            else if(!$(this).is(':checked'))
            {
                $('.checker').each(function(){
                    if($(this).val()=='20')
                    {
                       $(this).val('');
                    }
                });
            }
        });
    });
    

    You can see it work here : http://jsfiddle.net/w9hAZ/1/

提交回复
热议问题