【问题标题】:problems taking data from html form into jquery function将数据从 html 表单获取到 jquery 函数的问题
【发布时间】:2013-12-23 00:23:01
【问题描述】:

我在让我的 jquery 函数从我的输入 html 表单元素中获取我的数据时遇到了一些问题。

这是我的 javascript:

<script>
$(function(){


    $('#estimate').click(function() {

        var postcode_entry = $('#postcode_entry').value;

        $("#shipping_method").html("<div style=\'margin:20px auto;width:100px;text-align:center;\'><img src=\'ext/jquery/bxGallery/spinner.gif\' /></div>").show();

        $.get("checkout_shipping.php", {country: "38", refresh_quotes: "1", postcode: postcode_entry, zone_name: "canada" }, 
                function(data){
                    $("#shipping_method").html(data);
                }
            );



    });
});

</script>

html:

<input type="text"  name="postcode" id="postcode_entry" />
<button type="button" id="estimate" >Estimate..</button>

我无法将 postcode_entry 数据导入函数。如果我硬编码邮政编码,那么它可以正常工作。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您似乎正在使用 DOM 对象 上可用的属性访问 jQuery 对象 的值。

    对 jQuery 对象使用 .val(),对 DOM 对象使用 .value

    $('#postcode_entry').value;
    

    应该是

    $('#postcode_entry').val()  // accessing value of jQuery Object
    

    $('#postcode_entry')[0].value;  // accessing value of DOM object
    

    【讨论】:

      【解决方案2】:

      试试jQuery函数val(),而不是不存在的属性.value

      $('#postcode_entry').val()
      

      【讨论】:

        【解决方案3】:
        var postcode_entry = $('#postcode_entry').val();
        

        【讨论】:

          猜你喜欢
          • 2021-05-12
          • 1970-01-01
          • 1970-01-01
          • 2011-12-01
          • 1970-01-01
          • 2021-08-15
          • 2012-08-05
          • 1970-01-01
          • 2012-05-17
          相关资源
          最近更新 更多