使用JQuery传递输入隐藏值

时间:2014-04-10 00:07:43

标签: javascript jquery wordpress input hidden

我试图弄清楚为什么这段代码没有传递提交时隐藏输入字段的值。

代码取自WordPress模板页面,我已将其从所有特征WP标签中剥离,以便将其粘贴到此处。字段ID是“cutoff”。

<?php

$cutoffdate = $_POST['cutoff'];
$school = empty($_COOKIE['school']) ? null : $_COOKIE['school'];
$department = empty($_COOKIE['department']) ? null :  $_COOKIE['department'];

if ( ! empty($_POST['school']) ) {
setcookie('school', $_POST['school'], time() + 3600 * 24 * 30, '/');
setcookie('department', @$_POST['department'], time() + 3600 * 24 * 30, '/');

header('Location: '. $_SERVER['REQUEST_URI']);
exit;
}


if ( ! $school) {
    /*some code here */ }

elseif ($cutoffdate == "1") {
?> 
<div>
<form>
<select id="school" name="school">
<option value="97" >Art Center College of Design</option>
<option value="96" >Biola University</option>
<option value="4"  selected='selected'>California State University, Bakersfield</option>
<option value="22" >California State University, Pomona</option>
<option value="27" >California State University, San Marcos</option>
<option value="31" >Occidental College</option>
<option value="32" >Seminole State College of Florida</option>
<option value="89" >University of California, Davis</option>
<option value="90" >University of California, San Diego</option>
<option value="43" >University of Central Florida</option>
<option value="48" >University of Phoenix</option>
<option value="91" >Woodbury University</option>
</select>

        <input type="hidden" id="cutoff" name="cutoff" value="" />
        <br>
   </form>
    <?php endif; ?>



    </div>
    <script type="text/javascript">

jQuery( 

function ($) {

    $('#school').change( function (e) {

    var unihashtable = new Array();
        var unicutofftable = new Array();

        unihashtable[4] = 'California State University, Bakersfield';
        unihashtable[22] = 'California State University, Pomona';
        unihashtable[27] = 'California State University, San Marcos';
        unihashtable[31] = 'Occidental College';
        unihashtable[32] = 'Seminole State College of Florida';
        unihashtable[43] = 'University of Central Florida';
        unihashtable[48] = 'University of Phoenix';
        unihashtable[89] = 'University of California, Davis';
        unihashtable[90] = 'University of California, San Diego';
        unihashtable[91] = 'Woodbury University';
        unihashtable[96] = 'Biola University';

        unicutofftable[4] = new Date(2014, 0, 11, 21, 0, 0);
        unicutofftable[22] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[27] = new Date(2014, 4, 14, 21, 0, 0);
        unicutofftable[31] = new Date(2014, 4, 16, 21, 0, 0);
        unicutofftable[32] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[43] = new Date(2014, 3, 28, 21, 0, 0);
        unicutofftable[48] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[89] = new Date(2014, 5, 10, 21, 0, 0);
        unicutofftable[90] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[91] = new Date(2014, 4, 8, 21, 0, 0);
        unicutofftable[96] = new Date(2014, 4, 21, 21, 0, 0);


        for (var i in unihashtable) {


            if (i == $('#school').val()) {

                datecheck = new Date();
                if (datecheck > unicutofftable[i]) {
                    $("[name='cutoff']").val(1);
                    alert("Some Alert"); 
                    this.form.submit();


                    } else { this.form.submit(); }

            }

        }






    }   );


});
</script>
<?php }

else {
?>


<div>
<form>
<select id="school" name="school">
<option value="97" >Art Center College of Design</option>
<option value="96" >Biola University</option>
<option value="4"  selected='selected'>California State University, Bakersfield</option>
<option value="22" >California State University, Pomona</option>
<option value="27" >California State University, San Marcos</option>
<option value="31" >Occidental College</option>
<option value="32" >Seminole State College of Florida</option>
<option value="89" >University of California, Davis</option>
<option value="90" >University of California, San Diego</option>
<option value="43" >University of Central Florida</option>
<option value="48" >University of Phoenix</option>
<option value="91" >Woodbury University</option>
</select>
        <input type="hidden" id="cutoff" name="cutoff" value="" />
        <br>

    </form>
    <?php endif; ?>




</div>

<script type="text/javascript">

jQuery( 

function ($) {

    $('#school').change( function (e) {

    var unihashtable = new Array();
        var unicutofftable = new Array();

        unihashtable[4] = 'California State University, Bakersfield';
        unihashtable[22] = 'California State University, Pomona';
        unihashtable[27] = 'California State University, San Marcos';
        unihashtable[31] = 'Occidental College';
        unihashtable[32] = 'Seminole State College of Florida';
        unihashtable[43] = 'University of Central Florida';
        unihashtable[48] = 'University of Phoenix';
        unihashtable[89] = 'University of California, Davis';
        unihashtable[90] = 'University of California, San Diego';
        unihashtable[91] = 'Woodbury University';
        unihashtable[96] = 'Biola University';

        unicutofftable[4] = new Date(2014, 0, 11, 21, 0, 0);
        unicutofftable[22] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[27] = new Date(2014, 4, 14, 21, 0, 0);
        unicutofftable[31] = new Date(2014, 4, 16, 21, 0, 0);
        unicutofftable[32] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[43] = new Date(2014, 3, 28, 21, 0, 0);
        unicutofftable[48] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[89] = new Date(2014, 5, 10, 21, 0, 0);
        unicutofftable[90] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[91] = new Date(2014, 4, 8, 21, 0, 0);
        unicutofftable[96] = new Date(2014, 4, 21, 21, 0, 0);


        for (var i in unihashtable) {


            if (i == $('#school').val()) {

                datecheck = new Date();
                if (datecheck > unicutofftable[i]) {
                    $("[name='cutoff']").val(1);
                    alert("Some Alert"); 

                    this.form.submit();


                    } else { this.form.submit(); }

            }

        }






    }   );


});
</script>

<?php
};

我可能遗漏了一些基本或明显的东西,但我只是看不出为什么这不起作用......

1 个答案:

答案 0 :(得分:0)

是的,我弄清楚出了什么问题:

if ( ! empty($_POST['school']) ) {
setcookie('school', $_POST['school'], time() + 3600 * 24 * 30, '/');
setcookie('department', @$_POST['department'], time() + 3600 * 24 * 30, '/');

header('Location: '. $_SERVER['REQUEST_URI']);
exit;
}

此处的标头功能是重定向并终止$ _POST值。虽然我不能说100%它是标题本身而不是te退出这一切都搞砸了。如果知道更多的人可以解释,我会很高兴...