简体   繁体   English

从php传递可变形式的id到javascript

[英]passing a variable form id from php to javascript

I'm trying to pass a dynamically created id from a form to JavaScript. 我正在尝试将动态创建的ID从表单传递给JavaScript。

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    var fld = "<?= $div_id;?>";
    var test = $(fld).val();
        alert(test);
...

<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" maxlength="80" />
<input type="submit" value="Send" class="button-call" name="p_k2" id="p_k2" />
</form>

alert(test) shows: undefined. alert(test)显示:未定义。

when I change it to alert(fld) I get two alerts, first it shows an empty box, then the correct $div_id 当我将其更改为alert(fld)时,我收到两个警报,首先显示一个空框,然后显示正确的$ div_id

what am I doing wrong?? 我究竟做错了什么??

EDIT 1: I'm trying to display the text from the input... 编辑1:我试图显示输入中的文本...

EDIT 2: this is the complete JavaScript: 编辑2:这是完整的JavaScript:

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    //var fld = "<?= $div_id;?>";
    var fld = "input[name=<?=$div_id;?>]";
    //var fld = "#<?=$div_id;?>";
    var test = $(fld).val();
    var dataString = 'content='+ test;

    alert(fld);
    alert(test);

    if(test=='')
    {
        alert("Write some text ...");
    }
    else
    {
        $("#flash").show();
        $("#flash").fadeIn(1000).html('<img src="images/ajax-loader.gif" align="absmiddle"> <span class="loading"> Loading ...</span>');

      $.ajax({
      type: "POST",
      url: "p_k2.php",
      data: dataString,
      cache: false,
      success: function(html){
      $("#divToRefresh").after(html);
      document.getElementById(fld).value='';
      document.getElementById(fld).focus();
      $("#flash").fadeOut(1000);
      }
      });
    }
    return false;
  });
});
</script>

EDIT 3: this is the example I'm using: http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html everything works OK if I use fixed id. 编辑3:这是我使用的示例: http : //www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html如果使用固定ID,一切正常。 But can't get it working with dynamically created id. 但是无法使其与动态创建的ID一起使用。

EDIT 4: OK, there are many answers, but non of them seem to be working. 编辑4:好的,有很多答案,但似乎没有一个在起作用。 I'll try to simplify. 我会尝试简化。

I'll get back to this example, the one I started with (http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html). 回到我开始的示例(http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html)。

My website is something similar to Facebook. 我的网站类似于Facebook。 You have friends, and on your 'wall' page you have many different comment boxes (for each of your friends). 您有朋友,并且在“墙”页面上有许多不同的注释框(针对每个朋友)。 To make ID's of those comment boxes different from one another (to avoid posting something to all your friends) I had to use friend id's from database ( input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" ). 为了使这些注释框的ID彼此不同(以避免向您的所有朋友发布内容),我不得不使用数据库中的朋友ID( input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" )。 That part works OK. 那部分工作正常。

I can get that id to the JavaScript ( var fld = "input[name=<?=$div_id;?>]"; ) but, I can't read the value of that text field. 我可以在JavaScript中获取该ID( var fld = "input[name=<?=$div_id;?>]"; )),但是,我无法读取该文本字段的值。 Once I do that, I'll take care of inserting that to my database... 一旦这样做,我将负责将其插入数据库中。

var test = $("#"+fld).val();
alert(test);

try 尝试

var fld = "input[name=<?=$div_id;?>]";

or better 或更好

var fld = "#<?=$div_id;?>";

you can use $(this) inside the click event handler. 您可以在click事件处理程序中使用$(this)。

ie

$(".elementClass").click(function(){
    alert($(this).attr("id"));
});

will return the id of the .elementClass that was clicked. 将返回被点击的.elementClass的ID。

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

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