繁体   English   中英

如何确保如果在一个字段(其他)字段/下拉列表中输入了值,则必须填写/选择

[英]How to make sure that if value is entered in one field other (accompanying) field/dropdown list must be filled/selected

我有一个必须在其中输入区域的表格,然后必须从下拉列表中选择其单位。 我为此使用javascript验证,但无法正常工作。 这是我的代码:

function check_admin_post_property()
{
 var area=document.post_property.property_area.value;
if(area != "")
{
    if(document.post_property.area_unit.value == "Select")
    {
        alert("Area unit must be selected");
        document.post_property.property_area.focus();
        return false;
    }
}
}

第一行声明一个变量,并获取最终用户输入的值。 第二行检查该字段是否不等于空,表示该字段具有值。 第四行检查下拉列表中的值是否等于“选择”,因为用户必须选择适当的面积单位。 如果是,则选择验证失败。 基本上,我要输入的区域不是强制性的,但如果必须输入,则必须附带一个单位。

这是我的标记:

<form name="post_property" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<table>
    <tr><td>Other fields</td></tr>
    <tr>
    <td>Property area:</td>
    <td><input type="text" name="property_area" style="border:1px solid red;"><span style="color:red">*</span</td>
    </tr>
    <tr>
        <td>Area unit:</td>
        <td>
            <select name="area_unit">
                <option value="Select">Select</option>
                <option value="square_feet">Square feet</option>
                <option value="square_yards">Square yards</option>
                <option value="square_meter">Square meter</option>
                <option value="acre">Acre</option>
                <option value="hectare">Hectare</option>
            </select>
        </td>

    </tr>
    <tr>
         <td align="center"><input type="submit" name="submit" value="Submit" onclick="return check_admin_post_property()"></td>
</tr>

标记格式不正确,因为stackoverflow的标记无法正常工作。 在我这边甚至都不可见。

我正在导入具有脚本元素src属性的js。 它位于一个名为js的js文件夹中,该文件夹的js文件名为post_property.js。在该文件中,有一个函数check_admin_post_property()驻留在上面的代码中。 我无法发布完整的脚本和html文件,因为如果他们(我的工作人员)发现我会不在这里(此网站是我工作地点中访问次数最多的网站之一)。 但是我已经发布了相关部分。 如果您需要更多有关此信息,请告诉我或指向完成此操作的第三方页面。

您的代码的问题是它使用<input type='submit'> 您应该使用的是<input type='button'> 这是工作代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function check_admin_post_property() {
    var area=document.post_property.property_area.value;
    if(area != "")
    {
        if(document.post_property.area_unit.value == "Select")
        {
            alert("Area unit must be selected");
            document.post_property.area_unit.focus();
            return false;
        }
        else
            document.post_property.submit();
    }
    else {
        alert("Area must be entered.");
        document.post_property.property_area.focus();
    }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="post_property" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<table>
    <tr><td>Other fields</td></tr>
    <tr>
    <td>Property area:</td>
    <td><input type="text" name="property_area" style="border:1px solid red;"><span style="color:red">*</span></td>
    </tr>
    <tr>
        <td>Area unit:</td>
        <td>
            <select name="area_unit">
                <option value="Select">Select</option>
                <option value="square_feet">Square feet</option>
                <option value="square_yards">Square yards</option>
                <option value="square_meter">Square meter</option>
                <option value="acre">Acre</option>
                <option value="hectare">Hectare</option>
            </select>
        </td>

    </tr>
    <tr>
         <td align="center"><input type="button" name="submit" value="Submit" onclick="return check_admin_post_property()"></td>
</tr>
</table>
</form>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM