简体   繁体   English

如何获取HTML表单的输入标签ID?

[英]How to get input tag id of html form?

<?php
        $i = 1;
        $query1 = mysql_query("SELECT * FROM `alert_history` ORDER BY `alert_history`.`id` DESC LIMIT ".$start.",".$per_page."");
        while($result = mysql_fetch_array($query1)){
            echo '<td colspan = "2"><form method = "POST" onsubmit = "submitform()" ><textarea onFocus = "myFunction(1)" onBlur = "myFunction(0)" id = "comment'.$i.'" name = "comment"></textarea> <br />';
            echo '<input type = "text" id = "alertid'.$i.'" name = "alertid" value = "'.$result['id'].'">';
            echo '<input  type = "submit" name = "submit" value = "Comment" ></form></td>';
            $i++;
        }
?>


<script>
    function submitform(){
            var comment = $("#comment").val();
        var alertid = $("#alertid").val();
        alert(comment);
        $.ajax({
        type: "POST",
            url: "analytic.php",
            data:{cmt:comment,alert_id:alertid}
        });
            //return false;
      }
</script>

How to get textarea tag and input tag id for javascript function ? 如何获取textarea标签和javascript功能的input标签ID?
Both tag show in while loop so i want every textarea and input tag has different id. 两个标签都显示在while循环中,所以我希望每个textareainput标签都具有不同的ID。

If you generate dynamic id in text box that time if change something in text box that time store it's id in local variable and use it on in your code 如果您当时在文本框中生成动态ID,则在该时间更改文本框中的某些内容时,会将其ID存储在本地变量中并在代码中使用

<input type = "text" class="textBox" id = "alertid'.$i.'" name = "alertid" value = "'.$result['id'].'">';
var Id
  $(document).on('change', "input.textBox", function () {
             Id = $(this).attr("id");
alert(Id )
});

Hope this code can help you.. 希望这段代码可以对您有所帮助。

so first you set the different IDs and then you fetch it. 因此,首先设置不同的ID,然后再获取它。

var textAreaId = $('textarea').prop('id);
var inputId = $('input').prop('id);

Update 更新

$i is constant for every single set of textarea and input combined together. $ i对于每组文本区域和输入组合在一起是恒定的。

If you want to grab every set of textarea and input , you should rather assign the ID to instead of doing it for every textarea and input element. 如果要获取每个textareainput集合,则应该分配ID而不是为每个textarea和input元素分配ID。

<?php
        $i = 1;
        $query1 = mysql_query("SELECT * FROM `alert_history` ORDER BY `alert_history`.`id` DESC LIMIT ".$start.",".$per_page."");
        while($result = mysql_fetch_array($query1)){
            echo '<td colspan ="2" id='.$i.'"><form method = "POST" onsubmit = "submitform()" ><textarea onFocus = "myFunction(1)" onBlur = "myFunction(0)" name = "comment"></textarea> <br />';
            echo '<input type = "text" name = "alertid" value = "'.$result['id'].'">';
            echo '<input  type = "submit" name = "submit" value = "Comment" ></form></td>';
            $i++;
        }
?>



<script>
    function submitform(){
            var comment = $(this).closest('td').find('textarea').val();
            var alertid = $(this).parent().prop('id');
        alert(comment);
        $.ajax({
        type: "POST",
            url: "analytic.php",
            data:{cmt:comment,alert_id:alertid}
        });
            //return false;
      }
</script>

In jQuery, select the tag and use the .each() : 在jQuery中,选择标签并使用.each()

$("textarea").each(function(){
   alert($(this).attr("id"));
   alert($(this).val());
});
$("input").each(function(){
   alert($(this).attr("id"));
   alert($(this).val());
});

Demo here (Client side). 此处演示(客户端)。

Try this one: 试试这个:

( See JsFiddle working demo ) 请参阅JsFiddle工作演示

<?php
        $i = 1;
        $query1 = mysql_query("SELECT * FROM `alert_history` ORDER BY `alert_history`.`id` DESC LIMIT ".$start.",".$per_page."");
        while($result = mysql_fetch_array($query1)){
            echo '<td colspan = "2"><form method = "POST" onsubmit = "submitform(this)" ><textarea onFocus = "myFunction(1)" onBlur = "myFunction(0)" id = "comment'.$i.'" name = "comment"></textarea> <br />';
            echo '<input type = "text" id = "alertid'.$i.'" name = "alertid" value = "'.$result['id'].'">';
            echo '<input  type = "submit" name = "submit" value = "Comment" ></form></td>';
            $i++;
        }
?>


<script>
    function submitform(form){

      var comment = $(form).find("textarea").val();
      alert(comment);

      var alertid = $(form).find("input[name=alertid]").attr("id");
      alert(alertid);
      //Or if you mean:
      var alertid = $(form).find("input[name=alertid]").val();
      alert(alertid);

        $.ajax({
        type: "POST",
            url: "analytic.php",
            data:{cmt:comment,alert_id:alertid}
        });
            //return false;
      }
</script>

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

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