dongxuan1660 2019-04-01 11:10 采纳率: 100%
浏览 365
已采纳

如何获取复选框值onclick提交按钮?

In this code, I have multiple checkboxes i.e. name="recruiter_id" with multiple submit button. Now, What happens when I click on the first row submit button it alert data but when I click on the last row submit button then it shows wrong recruiter_id I don't know why? So, How can I get different recruiter_id onclick different submit button? Please help me.

<?php
    foreach($wishlist as $row)
    {
?>
        <tr>
            <td>
                <input type="checkbox" name="recruiter_id" value="<?php echo $row['rid']; ?>" class="recruiter_id">
            </td>
            <td>
                <input type="submit" id="<?php echo $row['job_id']; ?>" class="btn btn-danger job_id" value="Assign" />
            </td>
        </tr>
<?php
    }
?>
<script>
    $(document).ready(function() {
        $(".job_id").click(function(e){
            e.preventDefault();
            job_id = this.id;
            recruiter_id = $("input[name='recruiter_id']:checked").val();
            alert(job_id);
            alert(recruiter_id);
        });
    });
</script>
  • 写回答

3条回答

  • doubi1713 2019-04-01 11:54
    关注

    First of all, change the type of input to button instead of submit :

    <input type="button" id="<?php echo $row['job_id']; ?>" data-val="<?php echo $row['rid']?>" class="btn btn-danger job_id" value="Assign" />
    

    Then inside the click event go up to the parent tr and check id the related checkbox is checked using .closest() and :checked like :

    $(document).ready(function() {
      $(".job_id").click(function(e) {
        e.preventDefault();
    
        var job_id = this.id;
        var recruiter_id = $(this).closest('tr').find("input[name='recruiter_id']:checked").val();
    
        console.log('job ID : ' + job_id + ' --- Recruiter ID:' + recruiter_id);
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table border=1>
      <tr>
        <td>
          <input type="checkbox" name="recruiter_id" value="1" class="recruiter_id">1
        </td>
        <td>
          <input type="button" id="1" class="btn btn-danger job_id" value="Assign" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="recruiter_id" value="2" class="recruiter_id">2
        </td>
        <td>
          <input type="button" id="2" class="btn btn-danger job_id" value="Assign" />
        </td>
      </tr>
      <tr>
        <td>
          <input type="checkbox" name="recruiter_id" value="3" class="recruiter_id">3
        </td>
        <td>
          <input type="button" id="3" class="btn btn-danger job_id" value="Assign" />
        </td>
      </tr>
    </table>

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP