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

在这段代码中,我有多个复选框,即带有多个提交的 name =“recruiter_id”</ code> 按钮。 现在,当我点击提醒数据的第一行提交按钮时会发生什么,但是当我点击最后一行提交按钮然后显示错误 recruiter_id </ code>我不知道为什么? 那么,我怎样才能获得不同的 recruiter_id </ code> onclick </ code>不同的提交按钮? 请帮我。 </ p>

 &lt;?php 
foreach($ wishlist as $ row)
{
?&gt;
&lt; tr&gt;
&lt; td&gt;
&lt; input type =“checkbox”name =“recruiter_id”value =“&lt;?php echo $ row ['rid'];?&gt;” class =“recruiter_id”&gt;
&lt; / td&gt;
&lt; td&gt;
&lt; input type =“submit”id =“&lt;?php echo $ row ['job_id'];?&gt;” class =“btn btn-danger job_id”value =“Assign”/&gt;
&lt; / td&gt;
&lt; / tr&gt;
&lt;?php
}
?&gt;
&lt; script&gt; \ n $(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);
});
});
&lt; / script&gt; \ n </ code> </ pre>
</ div>

展开原文

原文

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>

doulian7305
doulian7305 先生,请看一下我试图解释更多@Nick
一年多之前 回复
dow46218
dow46218 当你点击分配按钮时,你想要所有检查的recruiter_id吗?为什么你有更多的提交按钮?你可以在你的问题中解释更多吗?
一年多之前 回复
douqianmin5367
douqianmin5367 首先转到父tr(通过jQuery的.parents(),然后在其中找到复选框输入。
一年多之前 回复
douan0729
douan0729 首先,您的提交按钮必须在for循环之外。你可以foreach($wishlist为$k=>$row)和name=“recruiter_id<?phpecho$k;?>”$(“input[name^='recruiter_id']:checked”)应该做的伎俩
一年多之前 回复

3个回答



首先,将输入类型更改为按钮</ code>而不是提交:</ p>

&lt; input type =“button”id =“&lt;?php echo $ row ['job_id'];?&gt;” data-val =“&lt;?php echo $ row ['rid']?&gt;” class =“btn btn-danger job_id”value =“Assign”/&gt;
</ code> </ pre>

然后在click事件中转到父 tr </ code>和check id使用 .closest()</ code>和:checked </ code>检查相关复选框,如:</ p>

</ p> < div class =“snippet”data-lang =“js”data-hide =“false”data-console =“true”data-babel =“false”>

\ r
  $(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);

});

}); </ code> </ pre>

 &lt; script  src =“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”&gt;&lt; / script&gt;

&lt; table border = 1&gt;

&LT; TR& gt;

&lt; td&gt;

&lt; input type =“checkbox”name =“recruiter_id”value =“1”class =“recruiter_id”&gt; 1

&lt; / td&gt; \ r
&lt; td&gt;

&lt; input type =“button”id =“1”class =“btn btn-danger job_id”value =“Assign”/&gt;

&lt; / td&gt;

&lt; / tr&gt;

&lt; tr&gt;

&lt; td&gt;

&lt; input type =“checkbox”name =“recruiter_id”value =“2”class =“ recruiter_id“&gt; 2

&lt; / td&gt;

&lt; td&gt;

&lt; input type =”button“id =”2“class =”btn btn-danger job_id“value = “分配”/&gt;

&lt; / td&gt;

&lt; / tr&gt;

&lt; tr&gt;

&lt; td&gt;

&lt; input type =“ 复选框“name =”recruiter_id“value =”3“class =”recruiter_id“&gt; 3

&lt; / td&gt;

&lt; td&gt;

&lt; input type =”button“id =“3”class =“btn btn-danger job_id”value =“分配”/&gt;

&lt; / td&gt;

&lt; / tr&gt;

&lt; / table&gt; </ code > </ pre>

</ div>

</ div>

</ div>

展开原文

原文

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>



试试这个,</ p>

 &lt;?php 
foreach($ wishlist as $ row)
{
?&gt;
&lt; tr&gt;
&lt; td&gt;
&lt; input type =“复选框”name =“recruiter_id”value =“&lt;?php echo $ row [' 摆脱'];?&gt;“ class =“recruiter_id”&gt;
&lt; / td&gt;
&lt; td&gt;
&lt; input type =“submit”id =“&lt;?php echo $ row ['job_id'];?&gt;” data-val =“&lt;?php echo $ row ['rid']?&gt;” class =“btn btn-danger job_id”value =“Assign”/&gt;
&lt; / td&gt;
&lt; / tr&gt;
&lt;?php
}
?&gt;
&lt; script&gt ;
$(document).ready(function(){
$(“。job_id”)。click(function(e){
e.preventDefault();
job_id = this.id;
recruiter_id = $(this).data(“val”);
//$ ("input[name='recruiter_id']:checked").val();
alert(job_id);
alert(recruiter_id) ;
});
});
</ code> </ pre>

</ p>
</ div>

展开原文

原文

try this one,

<?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']; ?>" data-val="<?php echo $row['rid']?>" 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 = $(this).data("val");
        //$("input[name='recruiter_id']:checked").val();
        alert(job_id);
        alert(recruiter_id);
    });
});



你可以尝试document.getElementById(“#yofid of checkbox”)。val()它会给你值</ p>

</ div>

展开原文

原文

you can try document.getElementById("#yourid of checkbox").val() it will give u the value

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐