我有兩個提交按鈕在我的表格:負荷記錄,而不提交按鈕
- 按鈕提交/保存表單,並插入輸入字段到數據庫。
- 按鈕用於根據下拉選項將記錄加載到已存在於數據庫中的表單中。例如更改數據庫中的現有記錄。
現在我知道如何提交表單而不需點擊提交按鈕:
onChange = "this.form.submit()"
但我想要的是去除「Load按鈕」來,如果我在掇做出選擇它會自動加載記錄,而不是使用上面的代碼提交整個表單。
任何想法如何實現這一目標?
我有兩個提交按鈕在我的表格:負荷記錄,而不提交按鈕
現在我知道如何提交表單而不需點擊提交按鈕:
onChange = "this.form.submit()"
但我想要的是去除「Load按鈕」來,如果我在掇做出選擇它會自動加載記錄,而不是使用上面的代碼提交整個表單。
任何想法如何實現這一目標?
如果您正在使用jQuery你可以加載數據上下拉的變化,像這樣:
$('form select').change(function() {
alert('You should do your data loading now');
});
謝謝,這是我可以用來實現我想要的東西。 – 2012-08-08 10:18:39
<html>
<head>
<script type="text/javascript">
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();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
而且getuser.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
它是如何工作Example。
如果你使用jQuery,那麼你可以通過檢查提交表單如果selecbox已被更改。 $('。select_boxs_class')。change(function(){('#forms_id')。submit(); });' – Jeemusu 2012-08-08 09:40:38