当我尝试更改onchange函数的值时,它只更新第一个而不是第二个。我想手动改变价值。在这个问题中你看到2和2等于4但是当我想改变第二行时就像显示4多个4相等16.当改变4为5然后它冲突第一行我想要在同一行中改变。请帮助解决这个问题。
test.php
<html>
<title>
Test
</title>
<script type="text/javascript">
function taxapply(){
var one=document.getElementById('one').value;
//alert(one);
var two=document.getElementById('two').value;
//alert(two);
var equal=one*two;
document.getElementById('equal').innerHTML=equal.toFixed(2);
document.getElementById('equals').value=equal.toFixed(2);
alert(equal);
}
</script>
<body>
<?php
require_once('DB.class.php');
$db = new DB();
$db->connect();
$result=$db->SelectTable("multiple","","");
?>
<table class=" table table-bordered table-hover" border="1" align="center" width="50%">
<tr>
<th><strong>Sr.No.</strong></th>
<th>one</th>
<th>two</th>
<th>Equal</th>
</tr>
<?php
if(count($result) > 0)
{
foreach($result as $rslist12){
?>
<tr>
<?php $i =$i+1;
?>
<td align="center" ><?php echo $i;?></td>
<td align="center"><input type="text" name="one" id="one" style="width:60px;" value="<?php echo $rslist12['one'];?>" onchange="taxapply()";></td>
<td align="center" ><input type="text" name="two" id="two" style="width:60px;" value="<?php echo $rslist12['two'];?>" onchange="taxapply()";></td>
<td align="center" ><label id="equal"><input type="text" name="equal" id="equal" style="width:50px;" value="<?php echo $rslist12['equal'];?>" onchange="taxapply()";></label></td>
</tr>
<?php
}
}
?>
</table>
</body>
<html>
DB.class.php
<?php
class DB {
protected $db_name = 'plus2net';
protected $db_user = 'root';
protected $db_pass = '';
protected $db_host = 'localhost';
function __construct() {
$connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
mysql_select_db($this->db_name);
return true;
}
public function connect(){
$connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
mysql_select_db($this->db_name);
return true;
}
public function processRowSet($rowSet, $singleRow=false)
{
$resultArray = array();
while($row = mysql_fetch_assoc($rowSet))
{
array_push($resultArray, $row);
}
if($singleRow === true)
return $resultArray[0];
return $resultArray;
}
public function SelectTable($table, $where="",$fieldarray="",$debug="") {
if ($fieldarray=="")
{
$f_list = "*";
}
else
{
$f_list = $fieldarray ;
}
$sql = "SELECT $f_list FROM $table ";
if( ! empty( $where ) )
$sql .= " WHERE $where";
if($debug==1){echo $sql;exit();}
$result = mysql_query($sql);
if( ! $result )
return 0;
return $this->processRowSet($result);
}
public function SelectSingle($table, $where,$fieldarray="",$debug="") {
if ($fieldarray=="")
{
$f_list = "*";
}
else
{
$f_list = $fieldarray ;
}
$sql = "SELECT $f_list FROM $table ";
if( ! empty( $where ) )
$sql .= " WHERE $where";
if($debug==1){echo $sql;exit();}
$result = mysql_query($sql);
if( ! $result )
return 0;
if(mysql_num_rows($result) == 1)
return $this->processRowSet($result, true);
}
public function Update($table,$where,$data,$debug="") {
foreach ( $data as $column=>$value )
{
if($value !="now()"){
$fv[] = "$column = \""."$value"."\"";
}else{
$fv[]= "$column = "."$value"."";
}
}
$fv_list = trim(implode(", ", $fv));
$sql = "UPDATE $table SET "."$fv_list"." WHERE $where";
if($debug==1){echo $sql;exit();}
mysql_query($sql) or die(mysql_error());
return true;
}
public function Insert($table,$data,$debug="") {
$columns = "";
$values = "";
foreach( $data as $column=>$value )
{
$field[] = $column;
if($value !="now()")
$values[] = "'$value'";
else
$values[] = "$value";
}
$columns = trim( implode(", ", $field) );
$values = trim( implode(", ", $values) );
$sql = "insert into $table ($columns) values ($values)";
if($debug==1){echo $sql;exit();}
mysql_query($sql) or die(mysql_error());
//return the ID of the user in the database.
return mysql_insert_id();
}
public function Delete($table, $condition)
{
$query = "DELETE FROM $table WHERE $condition";
$result = mysql_query( $query);
if( ! $result )
return 0;
return 1;
}
}
?>
数据库
CREATE TABLE IF NOT EXISTS `multiple` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`one` int(11) NOT NULL,
`two` int(11) NOT NULL,
`equal` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `multiple` (`id`, `one`, `two`, `equal`) VALUES
(1, 2, 2, 4),
(2, 4, 4, 16);
答案 0 :(得分:0)
您有多个具有相同值的ID,在这种情况下,浏览器会选择具有该ID的第一个元素
在您的问题元素中,其中包含id&#39; one&#39;和&#39;两个&#39;多次出现
document.getElementById('one').value
将始终为您提供第一个元素&#39; one&#39; ID
所以要么使id动态化,要么使用this
参数将对象传递给taxapply
函数
答案 1 :(得分:0)
您可以将以下代码用于动态ID
<script type="text/javascript">
function taxapply(i){
var one=document.getElementById('one'+i).value;
//alert(one);
var two=document.getElementById('two'+i).value;
//alert(two);
var equal=one*two;
document.getElementById('equal'+i).innerHTML=equal.toFixed(2);
document.getElementById('equals').value=equal.toFixed(2);
alert(equal);
}
</javascript>
这是您的HTML代码
<td align="center" ><?php echo $i;?></td>
<td align="center">
<input type="text" name="one" id="one<?php echo $i;?>" style="width:60px;" value="<?php echo $rslist12['one'];?>" onchange="taxapply(<?php echo $i;?>)";>
</td>
<td align="center" >
<input type="text" name="two<?php echo $i;?>" id="two" style="width:60px;" value="<?php echo $rslist12['two'];?>" onchange="taxapply(<?php echo $i;?>)";>
</td>
<td align="center" >
<input type="text" name="equal" id="equal<?php echo $i;?>" style="width:50px;" value="<?php echo $rslist12['equal'];?>" onchange="taxapply(<?php echo $i;?>)";>
</td>
</tr>
这种代码示例适合我。
答案 2 :(得分:0)
<html>
<title>
Test
</title>
<script type="text/javascript">
function taxapply(obj){
var id = obj.id;
var idNo = id.substr(id.indexOf('-')+1);
var one=document.getElementById('one-'+idNo).value;
var two=document.getElementById('two-'+idNo).value;
var equal=one*two;
document.getElementById('equal-'+idNo).innerHTML=equal.toFixed(2);
document.getElementById('equals-'+idNo).value=equal.toFixed(2);
}
</script>
<body>
<?php
require_once('DB.class.php');
$db = new DB();
$db->connect();
$result=$db->SelectTable("multiple","","");
?>
<table class=" table table-bordered table-hover" border="1" align="center" width="50%">
<tr>
<th><strong>Sr.No.</strong></th>
<th>one</th>
<th>two</th>
<th>Equal</th>
</tr>
<?php
if(count($result) > 0)
{
$i=0;
foreach($result as $rslist12){
?>
<tr>
<?php
$i =$i+1;
?>
<td align="center" ><?php echo $i;?></td>
<td align="center"><input type="text" name="one" id="one-<?php echo $i; ?>" style="width:60px;" value="<?php echo $rslist12['one'];?>" onchange="taxapply(this)";></td>
<td align="center" ><input type="text" name="two" id="two-<?php echo $i; ?>" style="width:60px;" value="<?php echo $rslist12['two'];?>" onchange="taxapply(this)";></td>
<td align="center" ><label id="equal-<?php echo $i; ?>"><input type="text" name="equal" id="equals-<?php echo $i; ?>" style="width:50px;" value="<?php echo $rslist12['equal'];?>" onchange="taxapply(this)";></label></td>
</tr>
<?php
}
}
?>
</table>
</body>
<html>