doupapin172773 2012-04-19 04:39
浏览 262
已采纳

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

When I click Submit, I get an error that the value is undefined in my javascript that I use to validate my form for the aForm.propertyid.

I'm thinking this is related to using a select and trying to verify a value was chosen, as I've not validated against a select before and my other forms all work fine.

The correct values are in the $_POST in lease_edita, but the checkForm(this) doesn't seem to pass anything to the javascript for validation - my checkForm(aValue) didn't get aValue.

I added an alert in my checkForm to display the value for error checking, and it's not getting a value. The onchange="checkProperty(this.value);" works correctly though and does get a value.

propertyid is undefined @/lease_jsLibrary.js

I'm hoping I'm posting enough code... I've been working on this for 2 days without finding a fix, and am hopeful for suggestions or ideas.

The form:

<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 contents:

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;
}

thanks in advance for tips, suggestions, and ideas..

this is fixed by changing to use: if (isEmpty(aValue.propertyid.value)) thank you!!!

  • 写回答

1条回答 默认 最新

  • doutong2132 2012-04-19 05:00
    关注

    you want to check the <select> tag named "propertyid" ?

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?