如何使用 Jquery 获取隐藏值?

How do I get a hidden value using Jquery?

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

<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;
    });
});

如上最后部分“感谢您的询问,我们会尽快与您联系,我想获得隐藏字段的产品价值?

只需在Jquery.use下面的代码中使用.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/

希望对您有所帮助

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

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

你可以使用

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

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

希望对您有所帮助