douqinlin5094 2015-01-15 12:40
浏览 141
已采纳

如何使用Jquery获取隐藏值?

I am trying to get a hidden field (product) value in my form and pass this through on the thank you page:

<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>

I have an ajaxform file as follows below:

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

antiSpam();

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

As above on the very last part "Thank you for your enquiry, we will contact you shortly, I would like to get the hidden field product value?

  • 写回答

3条回答 默认 最新

  • duangutang3940 2015-01-15 12:49
    关注

    Simply use .val() function in Jquery.use the code below

      <input type="hidden" name="product" id="product" class="product" value="<?php echo $page[3]; ?>" />
    
        <script>
        alert($("#product").val());
        </script>
    

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

    Hope this helps you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?