【问题标题】:get value with jquery使用 jquery 获取价值
【发布时间】:2018-10-06 02:36:37
【问题描述】:

我想用 jquery 从我的部分中获取一个值,但我不知道该怎么做... 我查看了一些示例并尝试了类似的方法,但没有奏效...

  <script>
      var variable = $("#section").val("showpage"); 
      alert(variable);
  </script>

对于本节:

<section id="section" showpage="works"></section>

我也试过了:

var variable = $(this).attr("showpage");

但也没有用。

有人可以帮助我吗?!谢谢。

【问题讨论】:

    标签: jquery variables get


    【解决方案1】:

    您可以通过多种方式从Html 元素中获取价值,以下是您可以做到的几种方式。

    要记住从DOM 获取值的几件事。这应该在页面加载函数$(document).ready(function(){}) 或click、change 等$('selector').click(function(){}) 之类的事件之后的内部@

    $(document).ready(function(){
    // Get value from method 1 using text();
    var val_1 = $('#get-value-method-1').text();
    console.log(val_1);
    
    // Get value from method 2 using html();
    var val_2 = $('#get-value-method-2').html();
    console.log(val_2);
    
    // Get value from method 3 using attr() / data();
    var val_3_1 = $('#get-value-method-3').attr('data-id');
    console.log(val_3_1 );
    var val_3_2 = $('#get-value-method-3').data('id');
    console.log(val_3_2);
    
    
    // Get value from method 4 using val();
    var val_4 = $('#get-value-method-4').val();
    console.log(val_4);
    
    
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="get-value-method-1">1000</div>
    <div id="get-value-method-2"><span> 2000</span></div>
    <div id="get-value-method-3" data-id="3000"></div>
    <input type="text" id="get-value-method-4" value="4000" />

    【讨论】:

    • 谢谢!这澄清了很多事情......这也帮助我通过控制台日志跟踪另一个错误!
    • 很高兴知道,这个答案对您有所帮助。快乐的编码伙伴.. !!
    【解决方案2】:

    只需使用这个:http://api.jquery.com/attr/。如果我们在你的代码上使用它,应该是这样的:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Test</title>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script>
    </head>
    
    <body>
    
        <section id="section" showpage="works"></section>
    
        <script>
    
            // $(document).ready() should be the main starting point. 
    
            $(document).ready(function()
            {
                var variable = $("#section").attr("showpage"); 
                alert(variable);
            });
        </script>
    
    </body>
    

    【讨论】:

      【解决方案3】:

      需要在dom加载后执行,即在$(document).ready()内执行

      另外,我建议你使用text() 而不是val()

        <script>
            $(document).ready(function(){
               $("#section").text("showpage"); 
            });
        </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-13
        • 2014-06-17
        • 1970-01-01
        • 2015-04-09
        • 2019-04-08
        • 1970-01-01
        • 2018-09-29
        • 1970-01-01
        相关资源
        最近更新 更多