【问题标题】:How to one change the value of the next input?如何更改下一个输入的值?
【发布时间】:2013-10-03 07:34:47
【问题描述】:

如何一对一更改下一个输入的值?

此代码检查全部或取消全部,但我想一对一地检查或取消选中,

jQuery(document).ready(function() { 
    jQuery('#topluekle').click(function() {  
        if(jQuery(this).attr('checked')) { 
            jQuery('input:checkbox').attr('checked',true); 
            jQuery('input:text').attr('value','E'); 
        } else { 
            jQuery('input:checkbox').attr('checked',false); 
            jQuery('input:text').attr('value','H');    
        } 
    }); 
}); 

示例代码:

<form>

    <? for($i=1;$i<=5;$i++) { ?>
        <input type="checkbox" id="pgun[]" name="pgun[]">
        <input size="1" type="text" name="degerler[]" id="degerler[]" value="H">
        <br />
    <? } ?>

    <label class="checkbox">
        <input type="checkbox" value="E" name="topluekle" id="topluekle">
        Check / Uncheck All *
    </label>

</form>

【问题讨论】:

    标签: php jquery arrays checkbox


    【解决方案1】:

    试试

    jQuery(function ($) {
        $('#topluekle').click(function () {
            $('input[name="pgun[]"]').prop('checked', this.checked);
            $('input[name="degerler[]"]').val(this.checked ? 'E' : 'H');
        });
    });
    

    【讨论】:

      【解决方案2】:

      val()点赞,

      jQuery('input:text').val('H');
      

      并使用prop() 代替attr() 喜欢

      jQuery(document).ready(function() { 
          jQuery('#topluekle').click(function() {  
              if(jQuery(this).prop('checked')) { 
                  jQuery('input:checkbox').prop('checked',true); 
                  jQuery('input:text').val('E'); 
              } else { 
                  jQuery('input:checkbox').prop('checked',false); 
                  jQuery('input:text').val('H');    
              } 
          }); 
      });
      

      如果您想更改每个复选框点击的值,那么您可以尝试,

      jQuery(document).ready(function() { 
          jQuery('input[name="pgun[]"]').click(function() {  
              var newVal=$(this).next('input:text').val()=='E' ? 'H' : 'E';
              $(this).next('input:text').val(newVal);
          }); 
      });
      

      【讨论】:

      • 嗨,感谢您的回答。我解决了 jQuery('input[type=checkbox]').change(function() { valim = jQuery(this).next('input:text').val(); if(valim=="E") { jQuery(this).next('input:text').val("H"); } else { jQuery(this).next('input:text').val("E"); } });
      【解决方案3】:

      会改变对应文本框的复选框值

      jQuery(document).ready(function() { 
          var txtObj = $('input:text');
          jQuery('input:checkbox').each(function(i){
              jQuery(this).click(function(){
                  if(jQuery(this).attr('checked')) {
                      $(txtObj[i]).val("E");
                  } else {
                      $(txtObj[i]).val("H");
                  }
              });
          });
      }); 
      

      【讨论】:

        猜你喜欢
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 2021-12-20
        相关资源
        最近更新 更多