نفرض ان لديك الزرين التاليين :
كود HTML:
<input name="button1" type="button" value="Action 1" onclick="retuen Func1();" />
<input name="button1" type="button" value="Action 1" onclick="return Func2();" />
قمنا بإضافة حدث onclick بحيث إذا ضغط الزائر على الزر الاول ,, يتم تنفيذ Func1
وإذا ضغط على الزر الثاني ,, يتم تنفيذ Func2
الآن نضيف دوال الجافا سكربت التي سوف تقوم بتغيير ال action
كود HTML:
<script language="Javascript">
<!--
function Func1()
{
document.Form1.action = "db.php"
document.Form1.submit();
return true;
}
function Func2()
{
document.Form1.action = "other.php"
document.Form1.submit();
return true;
}
-->
</script>
Form1 هو إسم الفورم ,, أياً كان
ونسند لقيمة ال action في كلا الحالتين القيمة التي نريد
هذا مثال كامل :
كود HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document
</head>
<script language="Javascript">
<!--
function Func1()
{
document.Form1.action = "db.php"
document.Form1.submit();
return true;
}
function Func2()
{
document.Form1.action = "other.php"
document.Form1.submit();
return true;
}
-->
</script>
<form name="Form1" id="form1">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input name="button1" type="button" value="Action 1" onclick="retuen Func1();" />
<input name="button1" type="button" value="Action 1" onclick="return Func2();" />
</form>
<body>
</body>
</html>
بالتوفيق