【问题标题】:How to return value to console in javascript?如何在javascript中将值返回到控制台?
【发布时间】:2013-07-31 10:58:48
【问题描述】:

我正在尝试编写一些 javascript,当用户单击相应的文本时,它将返回 toasty.png 和 bready.png 的值。我可以返回“Toast”和“bread”,但不能返回其他文本。有什么建议吗?

<script>
    $(document).on('vclick', '.changePageButton', function() {
        console.log(this.text);
        //console.log(value within the image)
    });
</script>

<a class="changePageButton" value="Toast" data-transition="slide">
    <input type = "hidden" name = "image" value = "toasty.png">
    <input type = "hidden" name = "video" value = "video1.mpg">
    test
</a>

<a class="changePageButton" value="bread" data-transition="slide">
    <input type = "hidden" name = "image" value = "bready.png">
    <input type = "hidden" name = "video" value = "video2.mpg">
    test
</a>

【问题讨论】:

    标签: javascript console terminal


    【解决方案1】:
    // Also did you mean "click"?
    $(document).on('click', '.changePageButton', function () {
        var inputs = {};
    
        console.log(this.text);
    
        $(this).children('input').each(function (v) {
            inputs[$(this).prop('name')] = $(this).val();
        });
    
        console.log(inputs);
        console.log(inputs.image);
        console.log(inputs.video);
    });
    

    【讨论】:

    • 然后您可以使用.get(x) 选择其中一个,或者您可以在.children() 参数中更具体。
    • 我刚刚编辑了上面的代码。如何更具体地使用 .children?
    • 您要退回哪个?全部?
    • 是的,不过是分开的。
    【解决方案2】:

    试试这个

    $(document).on('vclick','.changePageButton', function() {
    
         console.log($(this).find("input[type='hidden']").val());
    
         // if you want according to hidden field name
         console.log($(this).find("input[name='image']").val());
    
    });
    

    希望对你有帮助

    【讨论】:

    • 如果我只想要名为“image”的隐藏字段中的值怎么办?
    • 你可以使用 find("input[name='image']")
    【解决方案3】:

    可用于获取元素的表单标签

    <script>
     $(document).on('vclick','.changePageButton', function() {
      var frm = document.getElementById('ID');
      // jQuery frm = $("#ID")
      console.log(this.text);
      console.log(frm.image.value[0]);
      console.log(frm.image.value[1]);
      // or you can use loop  FOR, WHILE etc
     });
    </script>
    <form id="ID">
        <a class = "changePageButton" value = "Toast" data-transition="slide">
            <input type = "hidden" name = "image" value = "toasty.png">
            test
        </a>
    
        <a class = "changePageButton" value = "bread" data-transition="slide">
            <input type = "hidden" name = "image" value = "bready.png">
            test
        </a>
     </form>
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      相关资源
      最近更新 更多