我的复选框不适用于显示数据的循环(My checkbox doesn't work with loop for to display data)

编程入门 行业动态 更新时间:2024-10-24 22:25:39
我的复选框不适用于显示数据的循环(My checkbox doesn't work with loop for to display data)

我把复选框做得很好,这是我的一个:

$(document).ready(function(){ $('input[type="checkbox"]').click(function(){ if($(this).prop("checked") == true){ document.getElementById("srt").value = document.getElementById("Ultra").value; } else if($(this).prop("checked") == false){ document.getElementById("srt").value = ""; } }); });

在我的显示数据部分,我有循环显示所有记录。 我想点击复选框后我从document.getElementById("Ultra").value获取的值document.getElementById("Ultra").value并显示在document.getElementById("srt").value 。 它仅适用于1条记录,其余的我检查不起作用。 我认为问题是我将它显示在<input type="text" id="srt">和我放入循环的文本框中,以显示数据库。 有帮助吗?

这个是php部分:

for ( $v = 0 ; $v < mysql_num_rows($result) ; $v++ ) { $row = mysql_fetch_assoc($result); ?> <td><input type="checkbox"/></td> <?php echo'<td>'.$row['aaa'].'</td>'; echo'<td>'.$row['bbb'].'</td>'; echo'<td>'.$row['ccc'].'</td>'; echo'<td><input type="text" id="srt"></td>';//////this one to display value i get echo'<td>'.$row['dddr'].'</td>'; }

该值仅显示在1行上。

I did the checkbox work good, this is my one:

$(document).ready(function(){ $('input[type="checkbox"]').click(function(){ if($(this).prop("checked") == true){ document.getElementById("srt").value = document.getElementById("Ultra").value; } else if($(this).prop("checked") == false){ document.getElementById("srt").value = ""; } }); });

At my display data part, I have the loop for to show all the record. I want after I click the checkbox the value I get from document.getElementById("Ultra").value and display on document.getElementById("srt").value. It work good at 1 record only, the rest I check didn't work. I think the problem is I display it on <input type="text" id="srt"> and the textbox I put in loop with loop for to display database. Any help?

This one is php part:

for ( $v = 0 ; $v < mysql_num_rows($result) ; $v++ ) { $row = mysql_fetch_assoc($result); ?> <td><input type="checkbox"/></td> <?php echo'<td>'.$row['aaa'].'</td>'; echo'<td>'.$row['bbb'].'</td>'; echo'<td>'.$row['ccc'].'</td>'; echo'<td><input type="text" id="srt"></td>';//////this one to display value i get echo'<td>'.$row['dddr'].'</td>'; }

The value only display on 1 row only.

最满意答案

我看到的一件事是你在每个循环中都有<input type="text" id="srt"> 。 这意味着你的id不是唯一的,这可能会导致document.getElementById("srt").value = document.getElementById("Ultra").value;出现问题document.getElementById("srt").value = document.getElementById("Ultra").value; 呼叫。

尝试使make ID唯一(这意味着为每个文本框提供不同的id,可能类似于srt_'.$v.'并且还在jquery函数中检索正确的文本框id )

PS:你能否在php代码中显示“Ultra”输入?

编辑:要获得正确的文本框,您也可以为您的复选框设置一个ID,并使用它来获取文本框的正确ID。

在您的页面中:

echo'<td><input type="checkbox" id='.$v.'/></td>';

echo'<td><input type="text" id="srt'.$v.'"></td>';

在你的功能

if($(this).prop("checked") == true){ document.getElementById("srt"+this.id).value = document.getElementById("Ultra").value; }

我没有测试它,所以可能有一些东西需要调整,但这个想法就在那里。

One thing I see is you have <input type="text" id="srt"> in each loop. This means your ids are not unique and this could cause problems in the document.getElementById("srt").value = document.getElementById("Ultra").value; call.

Try make ids unique (this means giving a different id for every textbox, maybe something like srt_'.$v.' and also retrieving the correct textbox id in the jquery function)

PS: can you please show also the "Ultra" input in your php code?

Edit: to get the correct textbox you could set an id to your checkbox too and use it to get the correct id of the textbox.

In your page:

echo'<td><input type="checkbox" id='.$v.'/></td>';

and

echo'<td><input type="text" id="srt'.$v.'"></td>';

And in your function

if($(this).prop("checked") == true){ document.getElementById("srt"+this.id).value = document.getElementById("Ultra").value; }

I did not test it so there could be something to adjust, but the idea is there.

更多推荐

本文发布于:2023-07-27 17:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   不适用于   数据   checkbox   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!