2013-12-17 120 views
0

嗯,我有一個HTML表單驗證的JavaScript。在這種形式下有編輯器textarea框。當此框爲空時,顯示「頁面內容需要」的javascript警報消息。沒關係。Javascript空字段沒有正確驗證

但填充內容後,它再次向我展示「頁面內容需求」。我的代碼有什麼問題?

使用Javascript:

function doStart() 
    {  
    var page_name = document.page.page_name.value; 
    var page_loc = document.page.page_location.value; 

    <?php 

    $page_limit = mysql_query("SELECT menu_name FROM cms WHERE username = '$username' AND cms_location = 1 "); 
    $num_page_limite = mysql_num_rows($page_limit); 
    ?> 
    var num = <?php echo $num_page_limite; ?> 


    var page_content = document.page.editor_kama.value; 
    var uploadobj = document.getElementById('myuploader'); 


    if(page_name == null || page_name == "") 
    { 
     alert("page name require"); 
     document.page.page_name.focus() ; 
     return false; 
    } 
    else if(page_name.length > 15) 
    { 
     alert("page name is too long"); 
     document.page.page_name.focus() ; 
     return false; 
    } 

    if(page_loc == null || page_loc == "") 
    { 
     alert("Select page location"); 
     document.page.page_location.focus() ;  
     return false; 
    } 
    else if(num == 5 && page_loc == 1) 
    { 
     alert("You already creaed 5 pages for your top menu."); 
     return false; 
    } 

    if(page_content == null|| page_content == "")  
    { 
     alert("Page content require"); 
     return false; 
    } 

    if (uploadobj.getqueuecount() > 0) 
    { 
     uploadobj.startupload(); 
    } 
    else 
    { 
     alert("Please browse files for upload"); 
    } 

} 
</script> 

HTML形式:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" id="form1" name="page" onSubmit="return validate()"> 
    <table width="100%" border="0" cellspacing="10" cellpadding="0" style="float:left; position:relative;">   
    <tr> 
    <td valign="top">Page Content</td> 
    <td> 
    <textarea cols="80" id="editor_kama" name="editor_kama" rows="30" class="textarea"><?php if(isset($_POST['editor_kama'])) echo $_POST['editor_kama'];?></textarea> 
    <script type="text/javascript"> 
     //<![CDATA[ 

     CKEDITOR.replace('editor_kama', { 
      skin : 'kama' 
     }); 

     //]]> 
    </script> 
    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td><input type="submit" name="Submit" value="Create page" class="submit" id="submitbutton" onclick="doStart();return false;"/></td> 
    </tr> 
    </table>    
</form> 
+0

你有一個頁面,我們不能測試它取代page_content == null|| page_content == ""?你有沒有嘗試測試值的長度? – kevpoccs

+0

@kevpoccs不,我不知道。我該怎麼辦 ? – Alex

+0

@kevpoccs如果我插入長文本,然後它仍然顯示我「頁面內容需要」。 – Alex

回答

1

的解決方案是

var page_content = document.page.editor_kama.value; 

通過

var page_content = CKEDITOR.instances['editor_kama'].getData(); 

您的編輯器生成大量代碼,您必須使用文檔與它們進行交互。

編輯:

通過page_content.length == 0

+0

同樣的錯誤顯示... – Alex

+0

你有更新測試頁嗎? – kevpoccs

+0

是的,我更新了它。 – Alex