0

我有这段代码,当人们将需求字段留空时,我想制作一个警告消息框。

编码:

if (@ is_array($cfields)) {
    foreach ($cfields as $cf) {
        if (intval($cf['required']) == 1) {
            if ($cf['type'] == "text" || $cf['type'] == "textarea" || $cf['type'] == "date") {
            ?>
            if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }
            <?php

            }elseif ($cf['type'] == "select") {
            ?>
            if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }
            <?php

            } elseif ($cf['type'] == "checkbox") {
                //checkbox
            ?>
            if(vrvar.vrcf<?php echo $cf['id']; ?>.checked) {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }else {
                document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
                return false;
            }
            <?php

            }
        }
    }
}
?>

现在,当该字段为空时,文本仅变为红色。

有任何想法吗?:)

4

1 回答 1

0

添加这样的警报消息[见第 3 行]

if(!vrvar.vrcf<?php echo $cf['id']; ?>.value.match(/\S/)) {
            document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='#ff0000';
            alert('Please dont leave the field blank');// I have added here
            return false;
            }else {
            document.getElementById('vrcf<?php echo $cf['id']; ?>').style.color='';
            }
于 2013-08-22T12:08:41.083 回答