dsour68888 2014-09-26 18:58
浏览 18
已采纳

通过使用具有相同ID的多行更改optionsMenu中的值来选中该复选框

I am creating a Data table with following code

<?php 
foreach($redeemSales as $sale)
{ 
   ?>
   <tr class='clickableRow<?php echo $isRead ? '' : ' newrow' ?>' href='#'>
       <td><form><input type="checkbox" id="userSelection" name="userslection" ></form></td>
      <td><?php echo $sale["ring"];?></td>
      <td><?php echo formatFullDate($sale["soldDate"]) ?></td>
      <td><?php echo $sale["saleType"]; ?></td>
      <td>   
          <div class="col-lg-8">
              <select name="type" id="redeemOptions" class="form-control">
                <option value="None">None</option>
                <option value="CD">(CD)</option>
                <option value="Amex">American Express Card (Amex)</option>
            </select>
        </div>
      </td>
  </tr>
<?php }?>

I want to make it so if anyone change the option to any oe of CD Or Amex, it will set the selection to its row as checked.

jAVAScript code is here

<script language="javascript">
$(document).ready(function(e) 
{    
    $('#redeemOptions').change(function(){
        if($('#redeemOptions').val() == 'None') 
        {           
            document.getElementById("userSelection").checked = false;
        }
        else
        {
            document.getElementById("userSelection").checked = true;   
        } 
    });
});

</script>

As you can see that there is a for loop, so its rows getting added in a table. The above method works only for first row. If i change the options, it will set selection to Checked. But after first rows, no other row is showing that behavior. It is probably something to do with id of the elements.

How can one find out a solution so that other rows show same behavior. Secondly, if I have to get the ids or values of all rows that are "Checked" how will i do it in php

  • 写回答

2条回答 默认 最新

  • douhuang1852 2014-09-26 19:16
    关注

    the problem is that an id has to identify one single element on the page, and you are generating multiple items with the same id. Change your select to something like the following:

    <select name="type" class="form-control redeemOptions">
    

    And then change your javascript function to the following, to take advantage of the fact that "this" will equal the object that the change event is being fired from:

    $('.redeemOptions').change(function(){
        var menuChanged = $(this),
            parentForm = menuChanged.closest('form'),
            correspondingCheckbox = parentForm.find('input[name=userSelection]');
        if(menuChanged.val() == 'None') 
        {           
            correspondingCheckbox[0].checked = false;
        }
        else
        {
            correspondingCheckbox[0].checked = true;   
        } 
    });
    

    Finally, remove the id="userSelection" from the checkbox and just leave the name. It won't hurt the functionality but it is technically invalid because again you can only have one element of a given id on the page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端