簡體   English   中英

必填字段javascript將php中的下拉標簽視為已選選項

[英]required field javascript sees the label for dropdown in php as a selected option

我使用了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-->

當未選擇任何內容時,默認情況下(選擇)選項出現。 因此,當(選定)處於活動狀態時,客戶尚未選擇任何一個選項。 但是,即使客戶想要提交表單,該表單也允許他們這樣做,因為它會將(選擇)視為具有值的選項。 我如何確保除非選擇“商務,家庭或學生”選項之一,否則他們不能提交表格? 可以在此鏈接中找到實時示例

我用於“必需的filll函數”的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代碼

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

可以使用必需的HTML代替javascript

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM