【问题标题】:required field javascript sees the label for dropdown in php as a selected option必填字段 javascript 将 php 中的下拉标签视为选定选项
【发布时间】:2014-02-17 13:46:41
【问题描述】:

我使用了一个 php 表单和一个 jquery 脚本,因此如果客户留下任何未填写的字段,他们将无法继续。相关字段代码如下:

<div class="storagetype">
        <div class="input select">
         <div class="labelfortype"> <label for="storagetype">select your storage type</label></div><!--end of labelfortype class-->
                <select name="storagetype" id="storagetype">
            <option value="">(select)</option>
            <option value="business">business</option>
            <option value="domestic">domestic</option>
            <option value="student">student</option>
            </select>
    </div>
   </div><!--end of storagetype class-->

当没有选择任何内容时,默认情况下会出现(选择)选项。因此,当 (selected) 处于活动状态时,客户尚未选择其中一个选项。但是,如果客户想要提交表单,则表单允许他们这样做,因为它将(选择)视为具有值的选项。我如何确保除非选择“商业、国内或学生”选项之一,否则他们无法提交表格?现场示例可以在Link 中找到

我用于“必需的填充功能”的javascript代码如下:

 function formCheck(formobj){

    var x=document.forms["form1"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");

var fieldRequired = Array("storagetype", "location", "durationnumber", "duration", "txtDate",  "fname", "sname", "email", "number");
    // Enter field description to appear in the dialog box



    var fieldDescription = Array("Type of Storage",  "Post Code", "Duration Number", "Rental Duration Period",  "Rental Start Date", "First Name",  "Surname",  "Email Address", "Telephone Number");

    // dialog message
    var alertMsg = "Please complete all fields and enter a valid email address";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;

            case "text":
                case "textarea":
                if (obj.value == "" || obj.value == null || atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
                    alertMsg += "  " + " " + "\n";
                }
                break;

            default:
            }
            if (obj.type == undefined){
                var blnchecked = false;
                for (var j = 0; j < obj.length; j++){
                    if (obj[j].checked){
                        blnchecked = true;
                    }
                }
                if (!blnchecked){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
    }
// -->
</script>

这个 javascript 适用于所有文本字段,但只给出了我在示例中提到的下拉选项(例如 storaget 类型)的错误。

【问题讨论】:

    标签: javascript php forms field required


    【解决方案1】:

    在循环内更改您的验证 javascript 代码

    case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].value == ""){ ---- } 
    //replace .text with .value
    

    【讨论】:

    • .text 在您返回“(选择)”的情况下返回所选选项的文本。
    【解决方案2】:

    你可以使用需要的 html 而不是 javascript

    <select name="storagetype" id="storagetype" required>
    

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2011-07-14
      • 2022-01-14
      • 2021-07-07
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多