gpt4 book ai didi

javascript - php和javascript插入行并给出提示

转载 作者:可可西里 更新时间:2023-10-31 23:48:29 28 4
gpt4 key购买 nike

我想给每一行一个提示,现在我遇到了问题

如何将行号放入其中div id="txtHint" 变为 txthint1 *txthint2* txthint3

onchange="showUser(this.value,i) 这是行数..所以我可以使用 showuser 函数

JS :

function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var colCount = table.rows[0].cells.length;

for(var i=0; i<colCount; i++) {

var newcell = row.insertCell(i);

newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}

function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;

for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}


}
}catch(e) {
alert(e);
}
}
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}

HTML :

<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<form action="http://localhost/tes/index.php/form/prints" method="post">
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk[]"/></TD>
<TD><select name="a[]" id="a" onchange="showUser(this.value)">
<option value="">select</option>
<?php
$sql="select * FROM user";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{

?>
<option value="<?=$row['username']?>"><?=$row['username']?></option>
<?php
}
?>


</select>
</TD>
<TD>
<div id="txtHint"></div>
</TD>
</TR>
</TABLE>
<input type="submit"/>
</form>

最佳答案

您基本上是在处理事件注册问题。您可以将数据存储在 ID 中(您提到的 i 值)。但是您在取回它时遇到了麻烦,是吗?

尝试在此处阅读此主题:http://www.quirksmode.org/js/events_advanced.html

然后,一旦你得到了(基本上)尝试这样的事情:

function registerEvents() {
var showUser = function(e) {
if (!e) var e = window.event;
var target = e.target || e.srcElement;
if (target.nodeType == 3) // defeat Safari bug
target = target.parentNode;
console.log(target);
// Your code would go here
// ..probably in place of the console.log() line as well.
},
eventElements = document.getElementsByTagName("select"),
eventRegistrar;
if(eventElements.length > 0) {
for(var i=0;i<eventElements.lengthli++) {
eventRegistrar = eventElements[i].addEventListener || eventElements[i].attachEvent;
eventRegistrar(((eventRegistrar==eventElements[i].attachEvent)?"on":"")+"change", registerEvents);
}
}
}
var windowEventRegistrar = window.addEventListener || window.attachEvent;
windowEventRegistrar(((windowEventRegistrar==window.attachEvent)?"on":"")+"load", registerEvents);

或者您可以使用 jQuery,这会大大减少您编写的代码。

关于javascript - php和javascript插入行并给出提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19247924/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com