Javascript 根据输入数据,如果条件为真,则显示按钮
我有一个输入文本字段,我使用javascript和php从数据库中获取数据。 html通过javascript获取数据Javascript 根据输入数据,如果条件为真,则显示按钮,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我有一个输入文本字段,我使用javascript和php从数据库中获取数据。 html通过javascript获取数据 function showUser(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return; } else { if (window.XMLHttpReques
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","get.php?q="+str,true);
xmlhttp.send();
}
}
<input type="text" id="name" onblur="showUser(this.value)">
<input type="buton" name="">
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
get.phpto连接数据库以显示数据
<?php
$q = intval($_GET['q']);
$con=mysql_connect("localhost","root","") or die("Could not Connect with Sql");
mysql_select_db("dialog",$con) or die("Could connect to Database");
$sql="SELECT * FROM customer WHERE cx_number = '".$q."'";
$result = mysql_query($sql);
echo "<table>
<tr>
<th>cx_number</th>
<th>package</th>
<th>balance</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['cx_number'] . "</td>";
echo "<td>" . $row['package'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
现在我的问题是,如果包裹余额为零,如何显示增加包裹金额的按钮。您可以在您的div中更改它,如下所示:
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","get.php?q="+str,true);
xmlhttp.send();
}
}
<input type="text" id="name" onblur="showUser(this.value)">
<div id="txtHint">
<b>Person info will be listed here...</b></div>
在ajax文件中:
<?php
$q = intval($_GET['q']);
$con=mysql_connect("localhost","root","") or die("Could not Connect with Sql");
mysql_select_db("dialog",$con) or die("Could connect to Database");
$sql="SELECT * FROM customer WHERE cx_number = '".$q."'";
$result = mysql_query($sql);
echo "<table>
<tr>
<th>cx_number</th>
<th>package</th>
<th>balance</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['cx_number'] . "</td>";
echo "<td>" . $row['package'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>". if( $row['balance'] == 0) { '<input type="button" name="" value="your value"> } '. "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
您可以在div中更改它,如下所示:
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","get.php?q="+str,true);
xmlhttp.send();
}
}
<input type="text" id="name" onblur="showUser(this.value)">
<div id="txtHint">
<b>Person info will be listed here...</b></div>
在ajax文件中:
<?php
$q = intval($_GET['q']);
$con=mysql_connect("localhost","root","") or die("Could not Connect with Sql");
mysql_select_db("dialog",$con) or die("Could connect to Database");
$sql="SELECT * FROM customer WHERE cx_number = '".$q."'";
$result = mysql_query($sql);
echo "<table>
<tr>
<th>cx_number</th>
<th>package</th>
<th>balance</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['cx_number'] . "</td>";
echo "<td>" . $row['package'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>". if( $row['balance'] == 0) { '<input type="button" name="" value="your value"> } '. "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
如果每个用户都需要它,只需在get.php中替换即可
echo "<td>" . $row['balance'] . "</td>";
代码是这样的
$("#txtHint tr").each(function() { // cicle through each row
el=$this.find( "td:eq(2)" ) // get td with index==2.(firs==0, secon==1)...
if (parseFloat(el.html()<=0) { // check if it's 0 or lower
el.append( $( "Your button or link html here" ) ); // add button
}
});
如果每个用户都需要它,只需在get.php中替换即可
echo "<td>" . $row['balance'] . "</td>";
代码是这样的
$("#txtHint tr").each(function() { // cicle through each row
el=$this.find( "td:eq(2)" ) // get td with index==2.(firs==0, secon==1)...
if (parseFloat(el.html()<=0) { // check if it's 0 or lower
el.append( $( "Your button or link html here" ) ); // add button
}
});
为什么不创建一个父div并将该按钮放在该div中?是的,先生,我对此有点困惑。如果您不介意,请解释一下,您的ajax代码将始终返回一行或多行?您希望在哪里增加包按钮?@prakashtank sir它根据输入值返回整个数据列。为什么不创建一个父div并把那个按钮放在那个div里面?是的,先生,我对此有点困惑。如果你不介意的话,请解释一下你的ajax代码总是返回一行还是多行?你想在哪里增加包按钮?@prakashtank先生,它根据输入值返回整个数据列