JQUERY: изменить значение из текстового ввода, установив флажок

0

Мне нужно изменить значение текстового ввода, только если установлен флажок. Оба входа (текст и флажок) имеют один и тот же класс:

        <label for="text_to_apply">Write your text: </label>
        <input type="text" id="text_to_apply" name="text_to_apply" value="">
        <button type="button" id="btn_apply">Change</button>

        <form id="theform">
            <input type="text" value="1" id="one1" class="one"><input type="checkbox" id="one1_" class="one"><br>
            <input type="text" value="1" id="one2" class="one"><input type="checkbox" id="one2_" class="one"><br>
            <input type="text" value="1" id="one3" class="one"><input type="checkbox" id="one3_" class="one"><br>
            <input type="text" value="1" id="one4" class="one"><input type="checkbox" id="one4_" class="one"><br>
            <input type="text" value="2" id="two1" class="two"><input type="checkbox" id="two1_" class="two"><br>
        </form>
    </div>

    <script>
        $('#btn_apply').click(function() {
            var mytext = $('#text_to_apply').val();
            if ($('#theform').find('.one input:checked')) {
                $('#theform').find('.one:text').attr('value', mytext);
            }
        });
    </script>

</body>

У меня заканчиваются идеи. Пожалуйста помоги!

Благодарю!!

Теги:
input
text
checkbox

3 ответа

0
Лучший ответ

Если бы я правильно задал вопрос, это будет выглядеть так:

    $('#btn_apply').click(function() {
        if ($('#yourCheckboxId').is(':checked')){
             $('#inputId').val('some text you want');
        }            
    });

Вот скрипка http://jsfiddle.net/Goodluck/2XBcK/

0

Нет никакого :text в jQuery, о котором я знаю. Предполагая, что ваш HTML остается неизменным, вы можете использовать метод .prev() для таргетинга ввода перед каждым input:checked.

    $('#btn_apply').click(function() {
        var mytext = $('#text_to_apply').val();
        if ($('#theform').find('input:checked')) {
            $('#theform').find('input:checked').prev('input').attr('value', mytext);
        }
    });

Сценарий: http://jsfiddle.net/willthemoor/5qmH8/

0

Пытаться

    $('#btn_apply').click(function() {
        var mytext = $('#text_to_apply').val();
        $('#theform').find('input:checked').each(function(){
            $(this).prev().val(mytext);
        });
    });

DEMO

Ещё вопросы

Сообщество Overcoder
Наверх
Меню