如何在javascript中向控制台返回值

How to return value to console in javascript?

本文关键字:控制台 返回值 javascript      更新时间:2023-09-26

我正在尝试编写一些javascript,当用户单击相应的文本时,它将返回toasty.png和bready.png的值。我可以返回"Toast"answers"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>
// 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);
});

尝试这个

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

我希望它能帮助

可用于获取元素的Form标签

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