如何使用Jquery获取隐藏值?

时间:2015-01-15 12:40:58

标签: php jquery ajax

我正在尝试在我的表单中获取隐藏字段(产品)值并在感谢页面上传递:

<form action="" name="downloadform" id="downloadform"  class="downloadform" method="post">
        <div class="field">
        <input name="name" type="text" id="name" class="input name" placeholder="Name..." />
        </div>
        <div class="field">
        <input name="company" type="text" id="company" class="input company" placeholder="Company..." />
        </div>
        <div class="field">
        <input name="tel" type="text" id="tel" class="input tel" placeholder="Telephone..." />
        </div>
        <div class="field">
        <input name="email" type="text" id="email" class="input email" placeholder="Email Address..." />
        </div>
        <?php /*?><div class="field captcha">
        <img class="captcha_input" src="/inc/captcha.php" />
        <input name="captcha" type="text" id="captcha" class="input captcha" placeholder="Please enter characters...">
        </div><?php */?>
        <div class="field">
        <div class="medium secondary btn"><input type="submit" name="Submit2" value="Send Request" class="btn" id="downloadbtn" /></div>
        <input type="hidden" name="product" id="product" class="product" value="<?php echo $page[3]; ?>" />
        <input type="hidden" name="antiSpam" id="antiSpam" />
        </div>
    </form>

我有一个ajaxform文件,如下所示:

var antiSpam = function() {
    if (document.getElementById("antiSpam")) {
        a = document.getElementById("antiSpam");
        if (isNaN(a.value) == true) {
            a.value = 0;
        } else {
            a.value = parseInt(a.value) + 1;
        }
    }
    setTimeout("antiSpam()", 1000);
}

反垃圾邮件();

$(function() {

  filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  $("#downloadbtn").click(function() {

        var name = $("#name").val();
        var company = $("#company").val();
        var tel = $("#tel").val();
        var email = $("#email").val();
        var product = $("#product").val();
        var antispam = $("#antiSpam").val();

        if (name == "") {
          $("#name").focus();
          $("#name").val("");
          $("#name").css({background:"#b72a18", color:"#fff"});
          return false;
        }
        if (company == "") {
          $("#company ").focus();
          $("#company ").val("");
          $("#company ").css({background:"#b72a18", color:"#fff"});
          return false;
        }
        if (tel == "") {
          $("#tel").focus();
          $("#tel").val("");
          $("#tel").css({background:"#b72a18", color:"#fff"});
          return false;
        }
        if (!filter.test(email)) {
          $("#email").focus();
          $("#email").val("");
          $("#email").css({background:"#b72a18", color:"#fff"});
          return false;
        }
        if (product == "") {
          $("#product").focus();
          $("#product").val("");
          $("#product").css({background:"#b72a18", color:"#fff"});
          return false;
        }

        $('.downloadform').html('<center><img src="/images/ajax-loader.gif" style="padding:20px;"></center>');

        var dataString = '&name=' + name + '&tel=' + tel + '&company=' + company + '&email=' + email + '&product=' + product + '&antispam=' + antispam + '&type=download';
        //alert (dataString);return false;

        $.ajax({
            type: "POST",
            url: "/process.php",
            data: dataString,
            success: function() {

                setTimeout(function() {
                    $('.downloadform').html('<p>Thank you for your enquiry, we will contact you shortly. <a href="/pdfs/">Download</a></p>');
                }, 3500);
            }
     });
    return false;
    });
});

如上所述最后一部分&#34;感谢您的询问,我们会尽快与您联系,我想获得隐藏的现场产品价值?

3 个答案:

答案 0 :(得分:0)

只需在Jquery中使用.val()函数,然后使用下面的代码

  <input type="hidden" name="product" id="product" class="product" value="<?php echo $page[3]; ?>" />

    <script>
    alert($("#product").val());
    </script>

http://jsfiddle.net/3xscmrhz/1/

希望这有助于你

答案 1 :(得分:0)

你可以使用

ProductVal=$('#product').val();

ProductVal=document.getElementById("product").value;

我希望这个帮助

答案 2 :(得分:-1)

您可以像这样获得隐藏字段的值:

 var product = document.getElementById("product").value();