dssu33392 2016-06-27 18:28
浏览 56
已采纳

如何在使用循环创建的表单中选择输入字段的值

I need to set value of text box on change from select box created using jquery

 <?php for($i=0;$i<5 ;$i++) { ?>
<select  id="<?php echo 'aaa'.$i ?>" class="<?php echo 'aaa'.$i ?>">
                    <?php for($i=0;$i<5 ;$i++) { ?>
  <option value="1112" data-xyz="dynamic_value " data-abc="dynamic_value">dynamic_value</option>
            </select>
             <input  type="hidden"  class="<?php echo 'bbb'.$i ?>" id="bbb" name="<?php echo 'bbb'.$i ?>"/>
            <input type="hidden"   class="<?php echo 'ccc'.$i ?>" name="<?php echo 'ccc'.$i ?>" id="ccc" />
                       <?php } ?>
                         <?php } ?>






            <script>
$('.aaa').change(function () {
var otherValue=$(this).find('option:selected').attr('data-xyz');
var someOtherValue=$(this).find('option:selected').attr('data-abc');
$('.bbb').val(otherValue);
$('.ccc').val(someOtherValue);
});
</script>

How to change value class bbb0-bbb5 in jquery without using loop in jquery

  • 写回答

1条回答 默认 最新

  • dongquming3255 2016-06-27 18:32
    关注

    Do not update the class names in php-loop, classes need not be unique.

    To select input:hidden elements, no need to specify ID attribute

    $('.aaa').change(function() {
      var otherValue = $(this).find('option:selected').attr('data-xyz');
      var someOtherValue = $(this).find('option:selected').attr('data-abc');
      $(this).siblings('.bbb').val(otherValue);
      $(this).siblings('.ccc').val(someOtherValue);
    });
    
    <?php for($i=0;$i<5 ;$i++) { ?>
    <select id="<?php echo 'aaa'.$i ?>" class="aaa">
      <?php for($i=0;$i<5 ;$i++) { ?>
      <option value="1112" data-xyz="dynamic_value " data-abc="dynamic_value">dynamic_value</option>
    </select>
    <input type="hidden" class="bbb" name="<?php echo 'bbb'.$i ?>" />
    <input type="hidden" class="ccc" name="<?php echo 'ccc'.$i ?>" />
    <?php } ?>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到