2013-07-31 43 views
0

我想寫一些JavaScript,當用戶點擊各自的文本時,將返回toasty.png和bready.png的值。我能夠返回「吐司」和「麪包」,但不能包含其他文本。有什麼建議?如何在javascript中將值返回給控制檯?

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

回答

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); 
}); 
+0

如果我有內部的多個隱藏輸入字段標籤? – sharataka

+0

然後你可以使用'.get(x)'選擇其中的一個,或者你可以在'.children()'參數中更具體。 –

+0

我剛纔編輯了我的代碼。我如何更具體地使用.children? – sharataka

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

}); 

我希望這將有助於可用於獲取元素

+0

如果我只是想要在名稱爲「image」的隱藏字段中的值? – sharataka

+0

您可以使用find(「input [name ='image']」) –

+0

@sharataka根據名稱更新了我的答案 –

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> 
相關問題