我有這個程序,用戶輸入一個發票號碼,然後阿賈克斯去並填寫序列號,經銷商名稱,號碼和發票金額。Javascript功能多種形式
沒有靜態數量的發票。爲了這個例子的目的,有兩個invoice div,但可能會有更多或更少。我已經命名了我的表單和div,但是當涉及到輸入時,它們具有相同的名稱和ID。如果我有不同的ID,我想我需要太多,我不知道如何與一個Java腳本函數
代碼HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<script>
function dealerinfo() {
str= "detinv="+document.getElementById("detinv").value + "&dettype="+document.getElementById("dettype").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var splitResult = xmlhttp.response.split("/");
document.getElementById("detname").value=splitResult[0];
document.getElementById("detnum").value=splitResult[1];
document.getElementById("detamt").value=splitResult[2];
document.getElementById("detser").value=splitResult[3];
}
}
xmlhttp.open("POST", "deallookup.php");
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(str);
return false;
}
</script>
<div class="newboxes" id="newboxes1">
<form action="self.php" method="post" id="supplierform1" name="supplierform1">
Type: <input type="text" name="dettype" id="dettype" class="dettype" value = "C" >
<br>
Invoice Number: <input type="text" name="detinv" id="detinv" class="detinv" onblur="dealerinfo();">
<br>
Dealer Name :<input type="text" name="detname" id="detname" class="detname" ">
<br>
Amount:<input type="text" name="detamt" id="detamt" class="detamt" ">
<br>
Serial Number :<input type="text" name="detser" id="detser" class="detser" ">
<br>
Dealer Number:<input type="text" name="detnum" id="detnum" class="detnum" ">
<br>
<input type="submit" name="subbut" value="Submit" id="subbut">
<br><br><br>
</form>
</div>
<div class="newboxes" id="newboxes2">
<form action="self.php" method="post" name="supplierform2">
Type: <input type="text" name="dettype" id="dettype" class="dettype" value = "C" >
<br>
Invoice Number: <input type="text" name="detinv" id="detinv" class="detinv" onblur="dealerinfo();">
<br>
Dealer Name :<input type="text" name="detname" id="detname" class="detname" ">
<br>
Amount:<input type="text" name="detamt" id="detamt" class="detamt" ">
<br>
Serial Number :<input type="text" name="detser" id="detser" class="detser" ">
<br>
Dealer Number:<input type="text" name="detnum" id="detnum" class="detnum" ">
<br>
<input type="submit" name="subbut" value="Submit" id="subbut">
<br><br><br>
</form>
</div>
<br>
</body>
</html>
PHP代碼中引用它們
<?
$db = "SalesView";
include("msiconnect.php");
if($_POST["dettype"]=='C'):
$sql = "SELECT dba_name, dealer_no, serialno, balancedue from sales where invoiceno = ".$_POST["detinv"]."";
$result = $link->query($sql);
$r = $result->fetch_array();
if($result->num_rows > 0):
$dealerinfo =$r["dba_name"]."/".$r["dealer_no"]."/".$r["balancedue"]."/".$r["serialno"];
else:
$dealerinfo ="Invoice Number not Found";
endif;
endif;
if($_POST["dettype"]=='P'):
$sql = "SELECT dba_name, dealer_no, total from parts where invoiceno = ".$_POST["detinv"]."";
$result = $link->query($sql);
$r = $result->fetch_array();
if($result->num_rows > 0):
$dealerinfo = $r["dba_name"]."/".$r["dealer_no"]."/".$r["total"];
else:
$dealerinfo = "Invoice Number not Found";
endif;
endif;
echo $dealerinfo;
?>
謝謝,我知道這很好看。我確信我需要不同的ID,但是我不知道如何讓我的功能查找不同的IDS 也許是這樣的。
由於
不看你的代碼,「我不知道如何讓我的一次函數查找不同的ID」 - **不**。使用類而不是ID。 ''div class ='data-c'...'然後你可以通過類名'$(「。data-c」)...'來獲取所有匹配的類。類名不需要僅用於樣式,它們也可以用於分組類似元素或用於選擇(在這種情況下)。就個人而言,我用'data-'作爲前綴,以便清楚它是數據指示符,而不是樣式指示符,但這只是首選項 - 無所謂您稱之爲類[這可能與您的問題完全無關。 ] –