2012-04-19 82 views
-1

當我點擊提交,我得到一個錯誤,該值在我的javascript,我用它來驗證我的形式爲aForm.propertyid不確定的。onsubmit =「return checkform(this);」似乎是空的我的形式

我想這是關係到使用選擇,並試圖驗證值的選擇,因爲我不反對前選擇和我的其他形式都做工精細驗證。

正確的值在lease_edita中的$ _POST中,但checkForm(this)似乎沒有將任何事傳遞給javascript進行驗證 - 我的checkForm(aValue)沒有得到aValue。

我在checkForm顯示錯誤檢查增值警報,並且它沒有得到的值。 onchange =「checkProperty(this.value);」儘管工作正常,並確實獲得了價值。

物業ID未定義@/lease_jsLibrary.js

我希望我張貼足夠的代碼...我一直工作在這2天沒有找到一個解決方法,並且很希望尋求建議或想法。

形式:

<form name ="addEditLeaseForm" id="addEditLeaseForm" action="lease_edita.php" method="post" onsubmit="return checkForm(this)"> 
<?php 
$output = <<<HTML 
    <table class="center1" width="500"> 
     <tr> 
      <td style="text-align: right; "> 
      Property: 
      </td> 
      <td> 
      <select name="propertyid" id="propertyid" stle="width:270px;" onchange="checkProperty(this.value);" /> 
       <option value='' selected></option> 
HTML; 
       $propList = getPropertyList(); // get the properties to populate the list box 
       foreach ($propList as $aProperty) { 
       extract($aProperty); 
       $output .= <<<HTML 
       <option value="$idproperty" 
HTML; 
       if ($idproperty == $prop_id) { 
       $output .= <<<HTML 
selected 
HTML; 
      } 
       $output .= <<<HTML 
>$address</option> 
HTML; 
      } 
       $output .= <<< HTML 
      </select> 
      <font id="propertyErr" style="visibility: hidden; color:red; font-weight: bold;">*Required</font> 

      </td> 
     </tr> 
HTML; 
    echo $output; 
?> 
    <tr> 
    <td colspan ="2" align="center"> 
     <input type="submit" value="<?php echo $buttontext ?>" /> 
     <input type="button" name="Cancel" value="Cancel" onClick="history.go(-1);return true;" /> 
     </td> 
    </tr> 

    </table> 
</form> 

<p style="text-align: center"> 
    <input type="button" name="Property" value="Back to Property" onclick="window.location = '../property/property_main.php' " /> 
</p> 

lease_jsLibrary.js內容:

function trim(strToTrim) { 
    "use strict"; 
    //create a regular expression literal to identify leading and trailing spaces 
    var reg = /^\s+|\s+$/g; 
    //use the string method - replace - to remove leading and trailing spaces 
    return strToTrim.replace(reg,""); 
} 

// checks whether an input control is empty (i.e., the user failed to enter a required input) 
// note: it uses the trim method (see above) to eliminate white spaces before checking the input 
function isEmpty(aControl) { 
    return (trim(aControl.value).length == 0) ? true : false; 
} 

// checks for invalid characters in a string 
function hasInvalidChars(aControl) { 
    //create a regular expression literal to identify invalid characters 
    var reg = /[^a-zA-Z0-9\s\&\!\?\.',_-]/; 
    //use the regular expression method - test - to check whether the string contains invalid characters 
    return reg.test(trim(aControl.value)); 
} 

// removes starting and trailing white spaces 
function trimBlur(strToTrim) { 
    "use strict"; 
    //create a regular expression literal to identify leading and trailing spaces 
    var reg = /^\s+|\s+$/g; 
    //use the string method - replace - to remove leading and trailing spaces 
    return strToTrim.replace(reg, ""); 
} 

// checks whether an input control is empty (i.e., the user failed to enter a required input) 
// note: it uses the trim method (see above) to eliminate white spaces before checking the input 
function isEmptyBlur(aControl) { 
    "use strict"; 
    return (trimBlur(aControl).length === 0) ? true : false; 
} 

function checkProperty(aValue) { 
    "use strict"; 
alert(aValue); 
    if (isEmptyBlur(aValue)) { 
    document.getElementById("propertyErr").style.visibility='visible'; 
    document.getElementById("propertyid").style.borderColor='#FF3300'; 
    document.getElementById("propertyErr").innerHTML="*Required"; 
    return false; 
    } else { 
    document.getElementById("propertyErr").style.visibility='hidden'; 
    document.getElementById("propertyid").style.borderColor=''; 
    return true; 
    } 
} 

function checkForm(aValue) { 
    alert (aValue.propertyid); 
    if (isEmpty(aValue.propertyid)) { 
    document.getElementById("propertyErr").style.visibility='visible'; 
    document.getElementById("propertyid").style.borderColor='#FF3300'; 
    document.getElementById("propertyErr").innerHTML="*Required"; 
    return false; 
    } else return true; 
} 

在此先感謝的提示,建議和想法..

這是通過改變使用固定: 如果(的isEmpty(aValue.propertyid.value)) 謝謝!

+0

你應該檢查'aValue.propertyid'而不是'aForm.propertyid'嗎? 'aValue'似乎沒有被定義在任何地方。 – animuson 2012-04-19 04:45:17

+0

挑剔:'font'標籤已被棄用,不使用它們,這不是20世紀90年代。使用CSS類的跨度。連續多次查看'document.getElementById(「propertyErr」)是很糟糕的。將其存儲到一個變量中並使用它。 'var elem = document.getElementById(「propertyErr」); elem.style.visibility ='visible';' – epascarello 2012-04-19 04:45:28

+0

啊,我看到了..我改變了這一點,以便它們在checkForm(aValue) – Rahhstah 2012-04-19 05:10:05

回答

0
要檢查名爲「物業ID」的 <select>標籤

function checkForm(aForm) { 
    if (!aForm.propertyid.value ) { // so replace the line with this 
     document.getElementById("propertyErr").style.visibility='visible'; 
     document.getElementById("propertyid").style.borderColor='#FF3300'; 
     document.getElementById("propertyErr").innerHTML="*Required"; 
     return false; 
    } else { 
     console.log("submited"); 
     return false; 
    } 
} 
+0

中都是avalue當'textContent()'不僅僅是'innerHTML'時, /'innerText'應該被使用..而不是使用樣式屬性(每個**時間觸發[* Reflow *])(http://www.bitcurrent.com/browser-rendering-and-web-性能迴流/)在瀏覽器中),你應該添加一個類 – 2012-04-19 05:06:09

+0

謝謝!添加了這個,它的工作很棒...我需要更好的第二套眼睛。 – Rahhstah 2012-04-19 05:08:10

+0

另外:剛剛發現[JavaScript:表單驗證] [1],這也很好地解釋了這一點。再次感謝你的幫助,我會努力實施其他意見和反饋,我不想在1990年代:( [1]:http://www.the-art-of -web.com/javascript/validate/ – Rahhstah 2012-04-19 05:20:13