【问题标题】:how to get the clicked button event id如何获取被点击的按钮事件id
【发布时间】:2014-05-06 12:10:24
【问题描述】:

嗨,我需要在此表中获取事件 id,当单击 btnStopEvent 时,它会获取当前时间并将其显示到同一个表中,即 Endtime 列,例如我有 5 个事件 id 1、2、3、4 ,5 当用户单击第 2 列中的按钮时,它应该在 EndTime 列中显示当前时间,这是我现在所拥有的

function GetStartUserData() {
        var IPAddress = $('#IPAddress').text();
        (IPAddress == '' ? null : 'ipaddress=' + IPAddress)
        var EventId = '';
        var Category = $('#categories').val();
        var ExtraData = $('#txtcomment').val();

        return {
          MachineName: IPAddress
        , EventId: EventId
        , CategoryName: Category
        , Comments: ExtraData

        }
}


function DisplayStartData(downTimeStart) {
     console.log(downTimeStart);
     var newContent = '';

        $.each(downTimeStart.data, function (i, item) {
            newContent += Hesto.Html.StartTR(item.downTimeStart);
            newContent += Hesto.Html.CreateTD('<input type="button" value="Stop" id="btnStopEvent">');
            newContent += Hesto.Html.CreateTD(item.EventId);
            newContent += Hesto.Html.CreateTD(item.CategoryName);
            newContent += Hesto.Html.CreateTD(item.StartTime);
            newContent += Hesto.Html.CreateTD(item.EndTime);
            newContent += Hesto.Html.CreateTD(item.Comments);
            newContent  = Hesto.Html.EndTR(newContent);
        });
        $('#DowntimeList').append(newContent);     
 }

HTML:

<div id="panel"><table id="Downtimetable" class="hesto">
            <thead>
                 <tr>
                     <th>END OF DOWNTIME</th> 
                     <th>Event ID</th>                  
                     <th>CATEGORY NAME</th>             
                     <th>START TIME</th>
                     <th>END TIME</th>
                     <th>COMMENTS</th>
                 </tr>
             </thead>
             <tbody id="DowntimeList">

             </tbody>
             <tfoot>
             </tfoot>
        </table></div>
  <div class="label" id="IPAddress"><%Response.Write(Request.QueryString["ipaddress"]); %></div>

json 页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web.Script.Serialization;
using Hesto.SQL;
using Hesto;


public partial class services_json_DownTimeStartByMachineName : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
        nvc.AddFromQueryString(Request.QueryString);
        nvc.AddFromQueryString("MachineName", Request.UserHostAddress, Request.QueryString);
        nvc.AddFromQueryString("EventId", "NULL", Request.QueryString);
        nvc.AddFromQueryString("CategoryName","NULL",Request.QueryString);
        nvc.AddFromQueryString("StartTime",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),Request.QueryString);
        nvc.AddFromQueryString("Comments", "NULL", Request.QueryString);

        StoredProcedureCaller spc = new StoredProcedureCaller();
        spc.Execute(Request.QueryString, Resources.StoredProcedureDefinitions.DownTimeStartTimeByMachineName, Resources.ConnectionStrings.HESTOTESTING);

        Response.Write(spc.ToString("json"));
    }
}

json 页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web.Script.Serialization;
using Hesto.SQL;
using Hesto;

public partial class services_json_DownTimeStop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
        nvc.AddFromQueryString(Request.QueryString);
        nvc.AddFromQueryString("EventId","", Request.QueryString);
        nvc.AddFromQueryString("EndTime", DateTime.Now.ToString(), Request.QueryString);

        StoredProcedureCaller spc = new StoredProcedureCaller();
        spc.Execute(nvc, Resources.StoredProcedureDefinitions.DownTimeStopEvent, Resources.ConnectionStrings.HESTOTESTING);
        Response.Write(spc.ToString("json"));
    }
}

【问题讨论】:

  • 当点击 btnstopevent 按钮时,我希望它在新内容中显示当前时间 += Hesto.Html.CreateTD(item.EndTime);仅列。
  • 我很难理解你在这里想要做什么。如果您在 jsfiddle.net 上发布一个工作示例可能会有所帮助。
  • 用这个提供html代码,否则很难理解。

标签: javascript jquery


【解决方案1】:

你可以使用onclick方法:

<button onclick="myFunction(eventID)">Click me</button>

然后你可以将你的“事件ID”传递给JS部分:

function myFunction(eventID){
alert("Your Event ID : " + eventID);
}

或者你可以使用jquery:

$("button").click(function() {
    alert(this.id); // or alert($(this).attr('id'));
});

Getting ID of clicked element

【讨论】:

  • 当我单击按钮时说未定义的原因是按钮具有相同的 id $('#btnStopEvent')我如何将每个按钮分配在不同的行上,即 [tr] 具有不同的事件 id?
  • 你不能对多个按钮使用相同的 id,我不知道如何只使用 js,但可能你应该保留一个变量,将此变量作为文本部分添加到你的 id 名称, 并为每一行增加这个变量 - [tr],因此有唯一的 id。
  • 你可以试试这个:[HTML 元素的唯一标识符] (stackoverflow.com/a/3298600/2653036)
  • 你能给我一个 row[tr] 的例子
猜你喜欢
  • 2011-06-17
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2014-09-14
  • 2017-10-28
  • 1970-01-01
相关资源
最近更新 更多