0

我正在尝试编写一些 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>
4

3 回答 3

2
// 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);
});
于 2013-07-31T04:24:57.513 回答
0

尝试这个

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

});

我希望它会有所帮助

于 2013-07-31T04:27:26.040 回答
0

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

<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>
于 2013-07-31T04:32:57.893 回答