Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从表中检测th值单击 名称 住址 上下快速移动 300瓦克驱动_Javascript_Jquery - Fatal编程技术网

Javascript 从表中检测th值单击 名称 住址 上下快速移动 300瓦克驱动

Javascript 从表中检测th值单击 名称 住址 上下快速移动 300瓦克驱动,javascript,jquery,Javascript,Jquery,如果我单击Bob,我想返回值“Name”。如何使用jQuery实现这一点?是。点击td <table> <thead> <tr> <th>Name</th> <th>Address</th> </tr> </thead> <tbody> <tr>

如果我单击Bob,我想返回值“Name”。如何使用jQuery实现这一点?

是。点击
td

<table>
    <thead> 
        <tr>
            <th>Name</th>
            <th>Address</th>
        </tr>
    </thead> 
    <tbody>
        <tr>
            <td>Bob</td>
            <td>300 Wacker Drive</td>
        </tr>
    </tbody>
</table>

我这样做是为了在一个表上模拟一个ajax调用,但我认为这对您的情况非常有用

在您的情况下,您可以将
数据id
属性更改为
数据名=“bob”
,并使用
$target.attr(“数据名”)

html

CSS


快乐编码

你尝试过什么吗?离题:“询问代码的问题必须证明对正在解决的问题的最低理解。包括尝试过的解决方案,为什么不起作用,以及预期的结果。”结束时忘记了
。text()
。^忘记了获取文本(),你可能想展示如何添加单击<代码>$(“表tbody”)。在(“单击”,“td”,function(){…})上@ᾠῗᵲᄐᶌ 明白了..明白了..感觉很棒:)
var thVal = $(this).closest('table').find('th').eq( this.cellIndex).text();
alert(thVal);
<i style='display:none' id="loadingPopup">Loading</i>
<table>
    <tr>
        <td data-id="td1"> row 1</td>
    </tr>        
   <tr>        
        <td data-id="td2"> row 2</td>
    </tr>
    <tr>
        <td data-id="td3"> row 3</td>
    </tr>        
</table>
$("table").on("click", function(event){
    var $target = $(event.target); 
    if($target.is("td")){
        var id = $target.attr("data-id");
        makeAjax(id);
    }
});

//simulating ajax

function makeAjax(id){
    //you will have to use ajax an retrieve you json format
    //i will simulate ajax only
    $("#loadingPopup").show();
    var _json = { id : id, value : "some value", description : "some description"};
    setTimeout(function(){response(_json)}, 1000);
}

function response(_json){
    $("#loadingPopup").hide();    
    alert(_json.id + " - " + _json.value);
}
table{font-family:arial; font-size:12px; width:100%}
table tr td{border-bottom:1px solid #CCC; padding:10px; 100%}