如何在iframe裡對父級頁面進行表單驗證

來源:互聯網
上載者:User

我們有時候需要在iframe裡面對父級的表單進行驗證,這其實也很簡單,在iframe中使用parent關鍵字來調用父級頁面的javascript函數即可。下面是我在項目遇到的一個實際問題,為了以後使用方便,記錄下。如果能對大家也有用,那就最好了。

iframe表單設計:

<form name="add" method="POST" enctype="multipart/form-data" action="ecms.php" onsubmit="return chkinput(this);"><input type=hidden value=<?=$enews?> name=enews> <input type=hidden value=<?=$classid?> name=classid> <input name=id type=hidden id="id" value=<?=$id?>> <input type=hidden value="<?=$filepass?>" name=filepass> <input name=mid type=hidden id="mid" value=<?=$mid?>><?php  @include($modfile);?><input type="submit" name="addnews" value="發 布" class="login_button"></form>

在iframe裡寫上下面的驗證父級頁面的代碼:

<script type="text/javascript">function chkinput(form){//parent.CheckField();return parent.CheckField();}</script>

父級頁面的表單:

<form name="classid" id="classid" method="post" action="AddInfo.php" target="myFrame" onsubmit="return CheckChangeClass();"><select name="ClassSelector_Type:dbl_Class" id="ClassSelector_Type_dbl_Class" class="input" onchange="ResetClassItem()"><option value="50_0">請選擇大欄目</option><option value="58_1">房地產資訊</option><option value="61_1">教育培訓</option><option value="64_1">人才市場</option><option value="67_1">生活資訊</option><option value="70_1">吃喝玩樂</option><option value="73_1">交友徵婚</option><option value="75_1">本土商城</option><option value="76_1">車輛買賣</option><option value="85_1">家用電器/通訊數位</option><option value="86_1">裝飾/家居/建材</option><option value="87_1">金融商務</option></select><select name="ClassSelector_Type:dbl_ClassItem" id="ClassSelector_Type_dbl_ClassItem" class="input" onchange="ResetClassTypeTradeType(); document.classid.submit();"></select><div style="display:none;"><input name="ClassSelector_Type:txt_ClassId" type="text" value="50" id="ClassSelector_Type_txt_ClassId" /><input name="ClassSelector_Type:txt_TypeId" type="text" value="50_0" id="ClassSelector_Type_txt_TypeId" /><input name="ClassSelector_Type:txt_TradeType" type="text" value="2" id="ClassSelector_Type_txt_TradeType" /></div>(請選擇欄目,以便資訊容易審核通過)</form>

父級頁面的表單驗證代碼:

<script language="JavaScript">   function aa() {   alert("Ok!");   return false;}function CheckField(){var form_class_id = document.getElementById("ClassSelector_Type_txt_ClassId");//alert(form_class_id.value);if(form_class_id.value == '50' || form_class_id.value==''){alert("請選擇欄目");return false;}return true;}</script>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.