简体   繁体   English

如何使用jquery获取文本字段的val?

[英]how to get val of a text field using jquery ?

code: 码:

<script>
    $(document).ready(function(){
        $(".comments").keyup(function(){
            id = this.id;
            comment = $("#com"+id).val();
            alert(comment); 
        });
    });
</script>
<?php
    $this->db->select('*');
    $this->db->from('contact');
    $where = "admin_id like '%$client_id%'";
    $this->db->where($where);
    $query = $this->db->get();
    $result = $query->result_array();
    foreach ($result as $fet) 
    {
?>
    <tr>
        <td>
            <input type="text" name="comments" class="comments" id="com<?php echo $fet['id']; ?>" />
        </td>
        <td>
            <input type="submit" name="submit" id="submit" value="save" />
        </td>
    </tr>
<?php 
    }
?>

In this code I have multiple text field name comments and I want to alert value of multiple comments text field. 在这段代码中,我有多个文本字段名称注释,我想提醒多个注释文本字段的值。 Here, when I write something it show me undefine. 在这里,当我写东西时,它显示我未定义。 So, How can I get value of comments field using different id ? 那么,如何使用不同的id获取评论值的字段? Please help me. 请帮我。

Thank You 谢谢

You need to change, as this.id; 你需要改变,就像this.id; will return id , which would be com123 , but on top of that, you are adding com again, which will become comcom123 将返回id ,这将是com123 ,但最重要的是,你再次添加com ,这将成为comcom123

        id = this.id;
        comment = $('#'+id).val();

Other alternate solution is suggested by other as use this instead. 其他替代解决方案是由其他人建议使用this

Change your keyup event with this one. 使用此更改您的keyup事件。 this.id gives you com+id where id you set through php code. this.id为你提供com+id ,你通过php代码设置id

$(".comments").keyup(function(){
        id = this.id;
        comment = $(this).val();
        alert(comment); 
});

  $(document).ready(function(){ $(".comments").keyup(function(){ id = $(this).attr('id'); comment = $("#"+id).val(); alert(comment); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tr> <td> <input type="text" name="comments" class="comments" id="com1" /> </td> <td> <input type="submit" name="submit" id="submit" value="save" /> </td> </tr> 

Simply try this keyword, No need to go with id : 只需尝试this关键字,无需使用id

  $(document).ready(function(){ $(".comments").keyup(function(){ alert($(this).val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="comments" class="comments" id="com1" value="this is comment 1" /> <input type="text" name="comments" class="comments" id="com2" value="this is comment 2" /> <input type="text" name="comments" class="comments" id="com3" value="this is comment 3" /> 

So If i understood you correctly, you want to alert each time the user types anything with the value of that specific textbox? 因此,如果我理解正确,您希望每次用户键入具有该特定文本框值的任何内容时发出警报? Using jQuery, something like this should do it: 使用jQuery,这样的事情应该这样做:

 $("input[name=textbox1]").on("keyup", function() { window.alert($(this).val()); }); $("textarea[name=textbox2]").on("keyup", function() { window.alert($(this).val()); }); 
 .form-input { text-align: center; padding: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="POST"> <label>Textbox Example: </label><input type="text" name="textbox1" placeholder="Type Something.." class="form-input" /> <br /> <br /> <label>Textarea Example:</label> <textarea name="textbox2" placeholder="Type Something.." class="form-input"> </textarea> <br /> <br /> <input type="submit" name="submit" value="Submit" class="form-input" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM