如何在jquery中获取已传递参数的id

How to get the id of a passed arguement in jquery?

本文关键字:参数 id 获取 jquery      更新时间:2023-09-26

我有一个这样的函数,产品id将由一个名为productID的参数传递。product_id是一个隐藏字段,值将存储在其中。我有两个字段使用相同的函数。我想指定值来自哪个字段的函数,这样它就可以更新特定的隐藏字段。

 function transferId(productID)
{
    document.getElementById("product_id").value =productID; 
}

上面的函数来自toke输入自动完成,我们在隐藏字段中设置所选选项的值。

在上面的函数中,product_id是隐藏字段的id。

<input type="text" id="product_name" name="product_name" /> //field where the product is selected by autocomplete
<input type="hidden" id="product_id" name="product_id" /> // It stores the value product_id of the product selected.

当我每个表单有一个自动完成文本框时,它工作得很好,但现在我有两个自动完成的文本框。我需要传递给transferId函数的值的id,这样我就可以编写一个if条件来更新特定的隐藏字段。

更简单地说,我想取传递到函数中的值的ID。

使用以下代码

function transferId(productID)
{
    document.getElementById("product_id").value = productID.id; 
}

例如,我点击一个链接:

<a href="#" id="link_id" onclick="transferId(this)">delete</a>

该链路调用CCD_ 1。则CCD_ 3中的CCD_。可以经由CCD_ 4来访问与该链路相关的任何内容。由于我们传递了this,链接本身被传递给这个脚本,别名为el:

function transferId(el){
    alert(el.id);            // should give you "link_id"
    alert(el.href);          // should give you "#"
}

从这里,我们使用el.id以及其他属性来获得链接的id。

简而言之,只需通过事件中的this