添加id并使用它的jquery脚本不起作用 [英] jquery script adding id and using it doesn't work

查看:111
本文介绍了添加id并使用它的jquery脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的键盘使用id单击按钮。

i am trying to use my keyboard to click on a button using the id.

对于某些按钮,我必须设置ID,这实际上有效。但是一旦我尝试将键盘用于我设置id的按钮,它就无法工作。

For some buttons i had to set the ID, which actually works. But as soon as i try to use the keyboard for the buttons where i set the id, it won't work.

我没有收到任何错误,因为我添加了id到元素工作,我有点困惑为什么我不能在代码中使用新的set id。

I am not getting any errors and since adding the id to the element works, i am kinda confused why i cannot use the new set id later in the code.

//setting id for first button (works)
$( "a:contains('Im Verband freigeben')" ).attr('id', 'freigabe-verband');
//setting id for second button (works aswell)
$( "a:contains('Vorheriger Einsatz')" ).attr('id', 'vorheriger-einsatz');


$( document ).keydown(function(e) {
 if (e.keyCode == 39) {
    //works, id is available, not set by me
     $("#alert_next").click();

} else if (e.keyCode == 38){
    // doesn't work, but id is set
    $("#freigabe-verband").click();

} else if (e.keyCode == 40){
    // doesn't work, but id is set
    $("#vorheriger-einsatz").click();

} 
return e.returnValue;

});

任何人都知道为什么?
https://i.imgur.com/uy6oGFJ.png

Anyone know why? https://i.imgur.com/uy6oGFJ.png

推荐答案

要点击dom元素,请使用: -

To click a dom element, use:-

$('#freigabe-verband')[0].click();

$('#freigabe-verband').get(0).click();

示例

$("a").attr('id', 'test');

$(document).keydown(function(e) {
	$('#test')[0].click();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="http://stackoverflow.com">link</a>

这篇关于添加id并使用它的jquery脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆