0

除了验证是否选择了选择菜单中的选项外,我的表单上的所有内容都在工作。它会显示错误,但一旦选择了某些内容就不会消失

html

<div id="budget_input">
                <label for="budget">Budget</label>
            <select name="mydropdown">
            <option value="" id="mydropdown"> -- choose one --</option>
            <option value="firstchoice">$0 - $1,000</option>
            <option value="secondchoice">$1,000 - $2,000</option>
            <option value="thirdchoice">$3,000 +</option>
            </select>
</div>

php

$name = trim(stripslashes(htmlspecialchars($_POST['name'])));           
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$mydropdown = trim(stripslashes(htmlspecialchars($_POST['mydropdown'])));  

$error_message = '';    
    $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";

    if (!preg_match($reg_exp, $email)) {

                $error_message .= "<p>A valid email address is required.</p>";             
    }
    if (empty($name)) {

                $error_message .= "<p>Please provide your name.</p>";              
    }

    if (empty($mydropdown)) {

                $error_message .= "<p>Please select an item from the list.</p>";
    }   

js部分显示客户端

var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var mydropdown = $('form #mydropdown').val();   

if (name == '' || name.length <= 2) {
        valid = '<p>Your name' + required +'</p>';  
    }

    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
        valid += '<p>Your email' + required +'</p>';                                                  
    }

    if (mydropdown == '') {
        valid += '<p>An item from the list' + required +'</p>';

    }
4

3 回答 3

2

请用这个替换您的代码,希望它会起作用。

<div id="budget_input">
                    <label for="budget">Budget</label>
                <select name="mydropdown" id="mydropdown">
                <option value="" > -- choose one --</option>
                <option value="firstchoice">$0 - $1,000</option>
                <option value="secondchoice">$1,000 - $2,000</option>
                <option value="thirdchoice">$3,000 +</option>
                </select>
    </div>
于 2012-12-21T07:11:24.450 回答
2

更改您的标记

 <div id="budget_input">
            <label for="budget">Budget</label>
        <select id="mydropdown">
        <option value=""> -- choose one --</option>
        <option value="firstchoice">$0 - $1,000</option>
        <option value="secondchoice">$1,000 - $2,000</option>
        <option value="thirdchoice">$3,000 +</option>
        </select>
 </div>

改变你的 JS 代码

var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var mydropdown = $('form #mydropdown').val();   

if (name == '' || name.length <= 2) {
    valid = '<p>Your name' + required +'</p>';  
}

if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
    valid += '<p>Your email' + required +'</p>';                                                  
}

if (mydropdown == '') {
    valid += '<p>An item from the list' + required +'</p>';

}

只需添加您的选择框 ID,因为它用于在 Jquery 中检查是否选择了任何项目。


编辑#1-添加截图

截图——你做错的地方

于 2012-12-21T07:13:09.320 回答
1

你应该有 id=mydropdown 让 js 验证工作

 <select id="mydropdown">

现在您刚刚name="mydropdown"在选择框中设置了

于 2012-12-21T07:12:24.870 回答