onsubmit =“return checkform(this);”我的表格似乎是空的

时间:2012-04-19 04:39:44

标签: php javascript html

当我点击提交时,我收到一个错误,我的javascript中未定义该值,我用它来验证aForm.propertyid的表单。

我认为这与使用select并尝试验证选择的值有关,因为我之前没有对select进行验证,而我的其他表单都可以正常工作。

正确的值在lease_edita的$ _POST中,但是checkForm(this)似乎没有将任何内容传递给javascript进行验证 - 我的checkForm(aValue)没有得到aValue。

我在checkForm中添加了一个警告,显示错误检查的值,但没有得到值。 onchange =“checkProperty(this.value);”虽然工作正常,但确实有价值。

propertyid未定义@ / 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;
}

提前感谢提示,建议和想法..

通过更改使用来解决此问题:     if(isEmpty(aValue.propertyid.value)) 谢谢!!!

1 个答案:

答案 0 :(得分:0)

您想查看名为“propertyid”的<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;
    }
}