Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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 jquery和id选择器问题_Javascript_Jquery_Html_Random - Fatal编程技术网

Javascript jquery和id选择器问题

Javascript jquery和id选择器问题,javascript,jquery,html,random,Javascript,Jquery,Html,Random,我需要为页面上创建的一些按钮生成随机id。我使用以下jquery创建按钮: var btnId = 'delete-' + row.id; $("<button/>", { 'id': btnId, 'html': $("<i></i>").addClass("ion-icon ion-btn ion-trash-a"), 'class': 'btn btn-primary btn-md' }).append(' Delete') 情

我需要为页面上创建的一些按钮生成随机id。我使用以下jquery创建按钮:

var btnId = 'delete-' + row.id;
$("<button/>", {
    'id': btnId,
    'html': $("<i></i>").addClass("ion-icon ion-btn ion-trash-a"),
    'class': 'btn btn-primary btn-md'
}).append(' Delete')
情节越来越复杂 现在,我决定不使用数据库行id,而是生成一个随机字符串用作按钮id,因此我创建了以下函数:

function _randomString (min, max) {
    var text = "";
    var length = Math.floor(Math.random() * (max - min) + min);
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
}
如您所见,唯一不同的是,
$UUID
被我创建的这个函数的值替换

高潮 现在,当我使用jquery添加单击侦听器(与上面的jquery相同)时,绑定停止使用jquery。无论发生什么情况,jquery似乎都不希望在使用这样的id创建时找到并将单击侦听器绑定到按钮

起初,我认为这可能是由于函数没有生成随机字符串,但我去了控制台,获取了其中一个按钮的生成id,当我执行
$('copied-button-id')。length
,我得到了
1
。很明显,jquery现在能够找到按钮,但它不是在创建时找到的,就像我使用数据库行id创建按钮id时一样

更奇怪的是,当我删除随机生成的字符串并将其替换为普通字符串,如
sdfsdfsd
,即现在所有按钮都具有id
delete sdfsdfsd
,jquery在查找和绑定按钮方面没有问题

在任何人询问之前,我也仔细阅读了html元素的可接受ID,所以这不是一个问题

决议 这就是我需要你帮助的地方。我想我可能是真的很累或者是什么的,但是你能看看我的函数并告诉我是什么导致了这个问题吗。我已经消除了长度问题,因为我已经能够使用长度超过60个字符的ID,并且它们都有效。JQuery是否能够找到(或不找到)以这种方式创建的任何按钮,从而表现出未定义的行为?我是否表现出未定义的行为?我错过了什么

欢迎任何帮助


JQuery版本:2.1.4 MCVE:

$(函数(){
var numRows=10;
var$table=$(“#db table”);
对于(变量i=0;i

名称
年龄
行动

似乎根本没有必要为此使用ID。只需使用CSS类,将单击事件绑定到该类的所有按钮,如果需要执行与该按钮相关联的行或数据的特定操作,请在元素上使用html5
data
标记,或者只使用
$(this)。查找(…)
$(this)。最近(…)
在单击按钮时获取所需的任何信息

同样,这里的用例不是100%清楚,但是为了举例,我们假设您的button类是
awesome button

// Using $(document).on('click') instead of just
// $('.awesome-button').click() since you seem to be dynamically creating buttons.

$(document).on('click', '.awesome-button', function() {
  var row = $(this).closest('tr');

  // Now do whatever you want with the row...
  row.find('.something-obscure');

  // Or maybe your rows have data tags on them that need to go somewhere?
  someCrazyFunction(row.data('example'));

  // etc...
});

您正在
\u getButton()
函数中使用选择器
$(“button#'+btnId)
。但在函数返回并将按钮传递到
$row.append()
之前,按钮不会附加到DOM中

相反,只需绑定到元素本身,而不是使用选择器。
\u getButton()
需要返回元素本身,而不是它的
outerHTML
,因为这将丢失事件绑定

$(函数(){
var numRows=10;
var$table=$(“#db table”);
对于(变量i=0;i

名称
年龄
行动

为什么您需要ID?您可以创建元素并直接将事件处理程序绑定到元素。我看不出它不起作用的任何原因。您需要发布一个演示问题的MCVE,以便我们看到您做错了什么。它在这里起作用:当我复制您的代码时,我发现您缺少一个
$(“按钮#”+btnId)的末尾。单击(
行。@James,例如…?
$(“按钮”+btnId)。on('click',函数
var btnId = 'delete-' + _randomString(20, 30);
// Using $(document).on('click') instead of just
// $('.awesome-button').click() since you seem to be dynamically creating buttons.

$(document).on('click', '.awesome-button', function() {
  var row = $(this).closest('tr');

  // Now do whatever you want with the row...
  row.find('.something-obscure');

  // Or maybe your rows have data tags on them that need to go somewhere?
  someCrazyFunction(row.data('example'));

  // etc...
});