2014-02-23 77 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/

相關問題