2016-08-02 50 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> 
相关问题