2016-08-02 22 views
1

我想通過在顯示功能的文本框的值如何在show函數中傳遞文本框的值?

<div id="userComment"> 
    <input type="text" name="comment" id="comment" placeholder="Enter comment here"> 
    <button type="submit" class="postComment" onclick="show()" value="<?php echo $postId ?>">POST</button> 
</div> 
+0

你到目前爲止嘗試過什麼?這是一個非常簡單的任務,我只能假設你沒有花時間去做任何研究...... – NewToJS

+0

你可以通過它的id獲得文本框,然後獲得它的價值。 –

回答

3

當你與jQuery標註的問題,你可以註冊一個監聽器和值傳遞給show即可。無需使用元素上的onclick進行設置。

$(function() { 
 
    $(".postComment").click(function() { 
 
     show($("#comment").val()); 
 
    }); 
 
}); 
 

 
function show(value) { 
 
    console.log(value); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="userComment"> 
 
    <input type="text" name="comment" id="comment" placeholder="Enter comment here"> 
 
    <button type="submit" class="postComment">POST</button> 
 
</div>

1

我認爲,你可以寫這樣的代碼,這種方式非常簡潔。

<div id="userComment"> 
    <input type="text" name="comment" id="comment" placeholder="Enter comment here"> 
    <button type="submit" class="postComment" onclick="show($('#comment').val())" value="<?php echo $postId ?>">POST</button> 
</div> 
相關問題