Когда я пытался изменить значение on on function, он обновляется только один раз в секунду. Я хочу изменить значение в ручном режиме. в этой проблеме вы видите, что 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);
<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>
Вы можете использовать следующий код для динамического идентификатора
<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>
Этот тип кода работает для меня.
у вас есть несколько идентификаторов одинакового значения, и в этом случае браузер берет первый элемент, имеющий этот идентификатор
в ваших проблемных элементах с id'one 'и' two 'присутствуют несколько раз
document.getElementById('one').value
всегда будет иметь для вас первый элемент с идентификатором 'one'
поэтому либо сделайте идентификаторы динамическими, либо дайте ссылку на эти элементы, используя this
параметр, передавая объект функции taxapply
$i
которая не инициируется должным образом
onchange="taxapply('<?php echo $i;?>')"