我把复选框做得很好,这是我的一个:
$(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.
更多推荐
发布评论