【问题标题】:How to send the name attribute of <td> to a function?如何将 <td> 的名称属性发送到函数?
【发布时间】:2018-06-28 03:17:52
【问题描述】:

我有

  <td id='<?php echo $id;?>' name='StructuralDataLoaded' onclick='javascript:SelectStatus(id,name);'>
             <?php echo
                "<a>" . $arrayD['Structural Data Loaded'];. "<img   src='images/edit.png'></a>";
              ?>
            </td>

这是 SelectStatus 函数

       function SelectStatus(id,name) {

     console.log(id);
     console.log(name);

    var idvalue=id;
    var SelectingStatus = $('#SelectingStatus');
    SelectingStatus.dialog({
        close: function(event, ui) {

        },
        modal: true,
        title: 'Status' ,
        width: 400,
        height: 'auto',
        overlay: {
            backgroundColor: '#000000',
            opacity: 0.5
        },
        buttons: {
          'Save' : function PostData(){
    var data = $('#selected option:selected').val();
        $(this).dialog('destroy');
    var request = $.ajax({
    url: "InsertData.php",
    type: "POST",
    data: { 
    id : idvalue,
    data : data,
    type : name,
    },
    dataType: "html"
    });

    request.done(function( ) {
    location.reload();
    });
    request.fail(function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
    });
},
          'Return': function(){$(this).dialog('destroy');}
      }

    });// End dialog

 }

但控制台只显示id,不显示名称。我的错误是什么?以及如何将名称发送到 SelectStatus 函数?我正在为表格的其他单元格使用 SelectStatus 函数。所以我必须发送每个表的名称才能知道哪个单元格被编辑,然后更新数据库。

【问题讨论】:

  • 将“this”作为参数传递可能更容易,然后简单地获取属性(从函数内部)作为 var name = this.getAttribute("name");

标签: javascript php html-table


【解决方案1】:

你需要获取属性

onclick='javascript:SelectStatus(id,this.getAttribute("name"));'

【讨论】:

  • 谢谢,它有效。那么为什么发送 id 我可以只写 id,但我不能写 name?
  • 因为每个 DOM 元素都有一个id,所以 JavaScript 使用它。你也可以说 this.id 并且同样正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2020-09-01
  • 2018-06-06
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
相关资源
最近更新 更多