2014-02-23 76 views
0

仅当选中复选框时,才需要更改文本输入的值。两个输入(文字和复选框)具有相同的类:JQUERY:从选中复选框的文本输入更改值

 <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> 

我运行的想法。请帮忙!

谢谢!

回答

0

如果我收到了你的问题的权利 - 它看起来就像这样:

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

这里是小提琴http://jsfiddle.net/Goodluck/2XBcK/

0

尝试

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

DEMO

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/

相关问题