2013-12-13 38 views
0

我有三個PHP頁面。在第一個文件中,有兩個部門,其中一個部門包含超鏈接URL列表,第二個部門加載點擊超鏈接URL的輸出。下面是第一個文件的片段:如何在同一分區中加載多個PHP頁面

<script> 
$(document).ready(function() { 
$('a').each(function(){ 
$(this).on("click",function(e) { 
console.log(e); 
e.preventDefault(); 
$('#sidebar').load($(this).attr('href')); 
    }); 
}); 

</script> 

<div id="content"> Sidebar <p> &nbsp; </p> 
<div class="form"> 
<pre> 
<a href=sample_form1.php><b>Psychotic Drug</b></a><p> 
<a href=sample_form2.php><b>Antibiotic</b></a><p> 
<a href=sample_form3.php><b>Pain killer</b></a><p> 
<a href=sample_form4.php><b>Anti viral</b></a><p> 
</pre> 

</div> 

在第二個文件,我這是在第一個文件的第二師越來越打開的表單。下面是第二個文件的代碼片段:

function validateForm() 
{ 
var x=document.forms["drugForm"]["dname"].value; 
var y=document.drugForm.drug; 
//var y = new Array(); 

if (x==null || x=="") 
{ 
alert("First name must be filled out"); 
return false; 
} 
else if (Boolean(x)) 
{ 
for(k=0;k<y.length;k++) 
{ 
    if(y[k].checked) 
    { 
     alert(" " + y[k].value); 
     return true; 
    } 
} 
alert("Check one checkbox at least"); 
return false; 
} 

} 
</script> 


<form name="drugForm" action="form1.php" onsubmit="return validateForm()" method="post"> 
Drug name: <input type="text" name="dname"></pre> 
<pre> 

<input type="checkbox" name="drug" value="ID">DRUG-ID  <input type="checkbox" name="drug" value="Tree">Drug Tree</br> 
<input type="checkbox" name="drug" value="Node">Node  <input type="checkbox" name="drug" value="patent">Patent</br></br> 
    <input type="submit" value="Submit"> 
</pre> 
</form> 

我要加載的第一個文件的第二師這個文件,並希望發生的事情。但是每當我嘗試提交輸入時,表單仍然沒有響應。即表單輸入未被接受,並且onclick功能不起作用。請在這方面指導我如何在一個部門中加載多個PHP/HTML文件。

+0

您沒有表格結束標記 – Stewie

+0

我已經給出了Stewie。 –

回答

0

我猜你的表單中的內聯js不是通過jQuery的ajax函數($ .load只是一個快捷方式)來評估的。

你可以嘗試移動該部分無主文件,其內容如下:

<script> 
$(document).ready(function() { 
    $('a').each(function(){ 
     $(this).on("click",function(e) { 
      console.log(e); 
      e.preventDefault(); 
      $('#sidebar').load($(this).attr('href')); 
    }); 

    $(document).on('submit','form[name=drugForm]',function() { 
    var x=document.forms["drugForm"]["dname"].value; 
    var y=document.drugForm.drug; 
    if (x==null || x=="") { 
     alert("First name must be filled out"); 
      return false; 
     } else if (Boolean(x)) { 
      for(k=0;k<y.length;k++) { 
       if(y[k].checked)  { 
        alert(" " + y[k].value); 
        return true; 
      } 
     } 
     alert("Check one checkbox at least"); 
     return false; 
    }); 
}); 
</script> 

不過,在我看來,這是一個醜陋的方式來處理整個結構。有一些重大的重構正在進行。

相關問題