0

现在表单发送正确的信息但缺少状态。我正在使用 Ajax 成功函数。唯一的问题是该值不是固定的,而是取决于选择的状态。我该怎么办?

JS:

$('#sendFormBtn').live('click', function(){
        formdata = $('#submitForm').serialize();
        $.ajax({
                type: 'POST', 
                url: 'includes/contactForm.php',
                data: formdata, 
                success: function(){
                    $('input[name=name]').val('Name');
                    $('input[name=email]').val('Email');
                    $('input[name=address]').val('Address');
                    $('input[name=state]').val('');
                    $('input[name=city]').val('City');
                    $('input[name=zip]').val('Zip Code');
                    $('textarea[name=message]').val('Leave a message');
                }
        });
});

HTML:

<form id="submitForm">
    <table cellpadding="1" cellspacing="1">
        <tr>
            <td width="223">
                <input type="text" size="35" name="fullname" value="Full Name" onFocus="if(this.value == 'Full Name') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Full Name';}" />
                <br />
            </td>
        </tr>

        <tr>
            <td>
                <input type="text" size="35" name="youremail" value="Your Email" onFocus="if(this.value == 'Your Email') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Your Email';}" />
                <br />
            </td>
        </tr>

        <tr>
            <td>
                <input type="text" size="35" name="address" value="Address" onFocus="if(this.value == 'Address') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Address';}" />
                <br />
            </td>
        </tr>

        <tr>
            <td>
                <input type="text" size="35" name="city" value="City" onFocus="if(this.value == 'City') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'City';}" />
                <br />
            </td>
        </tr>

        <tr>
            <td>
                <p style="float:left; color:#FFF; padding-right:5px;">State/Zip</p>
                <select name="states">
                    <option value="alabama">AL</option>
                    <option value="alaska">AK</option>
                    <option value="arizona">AZ</option>
                    <option value="georgia">GA</option>
                    <option value="kentucky">KY</option>
                    <option value="louisiana">LA</option>
                    <option value="nebraska">NE</option>
                    <option value="nevada">NV</option>
                    <option value="new hampshire">NH</option>
                    <option value="new jersey">NJ</option>
                    <option value="new york">NY</option>
                    <option value="north carolina">NC</option>
                    <option value="washington">WA</option>
                    <option value="west verginia">WV</option>
                </select>

                <input type="text" size="8" maxlength="7" name="zip" value="Zip Code" onFocus="if(this.value == 'Zip Code') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Zip Code';}" />
            </td>
        </tr>

        <tr>
            <td>
                <textarea cols="28" rows="6" onblur="if(this.value=='') this.value='Leave a message'" onfocus="if(this.value =='Leave a message' ) this.value=''">Leave a message</textarea>
            </td>
        </tr>

        <tr>
            <td height="16">
                <input type="submit" name="submit_order" id="sendFormBtn" />
            </td>
        </tr>
    </table>
</form>
4

1 回答 1

0

听起来您使用错误的键来访问<select name="states">. $_POST['states']在您的 PHP 中,您应该查看$_POST['state'].

注意: <select name="states">不是<input>,所以如果你想用 jQuery 对它做点什么,你可以用$('select[name=states]'),而不是$('input[name=state]')

于 2012-10-03T17:00:01.940 回答