将以编程方式生成的值从listitem传递到jquery函数

时间:2021-06-18 20:36:49

I have a datatable-generated list with a certain set of attributes. I'd like to pass a certain attribute to a jquery function, which will then add the attribute to POST and then redirect the user to another page. I'm not sure if this is the best approach, but I'm having trouble pulling it off.

我有一个数据表生成的列表,其中包含一组特定的属性。我想将某个属性传递给jquery函数,然后将该属性添加到POST,然后将用户重定向到另一个页面。我不确定这是否是最好的方法,但我无法将其拉下来。

Right now I'm just trying to test how to actually pass the value to the jquery function. Here is the codebehind that generates the list items:

现在我只是想测试如何将值实际传递给jquery函数。以下是生成列表项的代码隐藏:

protected void BuildReportList(DataTable dt)
{
  string groupHeader = "";
  foreach (DataRow row in dt.Rows)
  {                
    ListItem myItem = new ListItem();
    myItem.Text = row["prg_menu"].ToString();
    myItem.Attributes.Add("title", row["prg_description"].ToString());
    myItem.Attributes.Add("onClick", "test();");
    myItem.Attributes.Add("value", row["prg_path"].ToString().Trim());
    reportsList.Items.Add(myItem);
  }
}

Here is the jquery function, which right now I'm just trying to pop an alert with the passed value:

这是jquery函数,现在我只是尝试使用传递的值弹出警报:

function test() {
  alert(($this).attr("value"));
}

Any advice on what I'm doing wrong here?

关于我在这里做错了什么的建议?

1 个解决方案

#1


1  

Try this way:

试试这种方式:

myItem.Attributes.Add("onClick", "test(this);");

And:

和:

function test(item) {
  alert($(item).val());
}

There are more options to check element' value:

有更多选项可以检查元素的值:

$(item).attr("value");

or without jQuery wrapping:

或没有jQuery包装:

item.value;

#1


1  

Try this way:

试试这种方式:

myItem.Attributes.Add("onClick", "test(this);");

And:

和:

function test(item) {
  alert($(item).val());
}

There are more options to check element' value:

有更多选项可以检查元素的值:

$(item).attr("value");

or without jQuery wrapping:

或没有jQuery包装:

item.value;