我正在尝试编写一些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>
答案 0 :(得分: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);
});
答案 1 :(得分: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());
});
我希望它会有所帮助
答案 2 :(得分: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>