简体   繁体   English

如何将每个功能或方法分配给按钮?

[英]How can i assign each function or method to a button?

Note: I'm still new in programming. 注意:我还是编程新手。

I have a JavaScript page that creates toggle buttons when the page loads, but i cannot use those buttons independently now because the id of the button is a column name on my database table. 我有一个JavaScript页面,在页面加载时创建切换按钮,但我现在不能单独使用这些按钮,因为按钮的id是我的数据库表上的列名。 so i cannot know it beforehand, that is why i cannot assign a method or function on each button. 所以我事先无法知道,这就是为什么我不能在每个按钮上分配方法或功能。

This is how the toggle button is created : 这是切换按钮的创建方式:

function createButtons(tbID, tbClass, tbType, tbValue, onClick) {
  return '\n<input' + (tbID ? ' id=\'' + tbID + '\'' : '') + 
    (tbClass ? ' class=\'' + tbClass + '\'' : '') + 
    (tbType ? ' type=\'' + tbType + '\'' : '') + 
    (tbValue ? ' value=\'' + tbValue + '\'' : '') + 
    (onClick ? ' onclick=\'' + onClick + '\'' : '') + '>';

}

function DisplayButtons(cableData) {
  var newContent = '';
  $.each(cableData, function (i, item) {
    function toggle() {
      $("#tb" + item.CommonCable)
        .clicked ? $('#MyElement')
        .addClass('clickedButton');
      $("#tb" + item.CommonCable)
        .removeClass("clickedButton");
      $('#MyElement')
        .toggleClass('MyClass');
    }
    newContent += createButtons("tb" + item.CommonCable, 
          null, "submit", item.CommonCable, toggle());
  });
  $("#Categories")
    .html(newContent);
}

NOTE : Anyone who can help asap ,how can i assign each function or method to each button ? 注意:任何可以尽快帮助的人,我如何为每个按钮分配每个功能或方法? : Or any other way i can use to create my toggle buttons ? :或者我可以使用任何其他方式来创建我的切换按钮?

You need something like this: 你需要这样的东西:

function createButtons(tbID, tbClass, tbType, tbValue, onClick) {
    var btn = $('<input>', {id:(tbID ? tbID : ''),
                         "class":(tbClass ? tbClass : ''),
                         "type":(tbType ? tbType : ''),
                          value:(tbValue ?  tbValue : ''),                        
                         });
     if(onClick)
         btn.click(onClick);
     return btn;
} 
function toggle() {
      $(this)
        .clicked ? $('#MyElement')
        .addClass('clickedButton');
      $(this)
        .removeClass("clickedButton");
      $('#MyElement')
        .toggleClass('MyClass');
    }
function DisplayButtons(cableData) {
  var newContent = [];
  $.each(cableData, function (i, item) {        
    newContent.push(createButtons("tb" + item.CommonCable, 
          null, "submit", item.CommonCable, toggle));
  });
  $("#Categories").empty()
    .append(newContent);
}

See how button is created in jquery style. 了解如何以jquery样式创建按钮。 Also, when you add onclick, you should pass pointer to a function you want to call on click, like here: createButtons("tb" + item.CommonCable, null, "submit", item.CommonCable, toggle) . 此外,当你添加onclick时,你应该将指针传递给你想要点击的函数,例如: createButtons("tb" + item.CommonCable, null, "submit", item.CommonCable, toggle) when you do toogle() , like in your code, you immediately execute your function and value returned by that function is used as onclick value. 当您执行toogle() ,就像在代码中一样,您立即执行您的函数,并将该函数返回的值用作onclick值。 Also, take a look how newly created button are added to array and than added to #categories element with .append method. 另外,看看如何将新创建的按钮添加到数组中,然后使用.append方法添加到#categories元素中。 Not sure if you need .empty , but that will work completely like your .html(newContent). 不确定你是否需要.empty ,但这将完全像你的.html(newContent)。 If you have some content in categories which should stay there, just remove empty 如果您有类别中的某些内容应该留在那里,只需删除empty

UPD Also, please note that you do not need to create an new toggle function for each new button. UPD此外,请注意,您无需为每个新按钮创建新的切换功能。 If you apply it like .click(toogle), this in toggle function will always point to clicked button and you do not need to find it using id. 如果您像.click(toogle)一样应用它,则切换功能将始终指向单击按钮,您无需使用ID找到它。 Not sure what is .clicked in your code. 不确定你的代码中是什么.clicked。 As far as I know, jQuery object has nothing like that. 据我所知,jQuery对象没有这样的东西。

Why don't you generate a unique id for each button created? 为什么不为每个创建的按钮生成唯一id

Something like: 就像是:

 $.each(cableData, function (i, item) {
    function toggle() {
      $("#tb" + i + item.CommonCable)
        .clicked ? $('#MyElement')
        .addClass('clickedButton');
      $("#tb" + i + item.CommonCable)
        .removeClass("clickedButton");
      $('#MyElement')
        .toggleClass('MyClass');
    }
    newContent += createButtons("tb" + i + item.CommonCable, 
          null, "submit", item.CommonCable, toggle());
  });

But I think you are in the wrong direction. 但我认为你的方向是错误的。

You can add the same class name on each button, for instance: tableButton , and then assign an event for all the element with this class name: 您可以在每个按钮上添加相同的类名,例如: tableButton ,然后为具有此类名的所有元素分配一个事件:

  $(".tableButton").on("click", function (event) {
    $('#MyElement').addClass('clickedButton').toggleClass('MyClass');
    $(this).removeClass("clickedButton");
  });

Then you can, when jQuery is ready, generate your button without any onclick action (already binded by the previous function) and with the class name: 然后,当jQuery准备就绪时,您可以生成按钮而不需要任何onclick操作(已经由前一个函数绑定)并且具有类名:

  $.each(cableData, function (i, item) {
    newContent += createButtons(".tableButton", 
          null, "submit", item.CommonCable);
  });

And the final code will look like this: 最终的代码如下所示:

$(function () {
  $(".tableButton").on("click", function (event) {
    $('#MyElement').addClass('clickedButton').toggleClass('MyClass');
    $(this).removeClass("clickedButton");
  });

  var newContent = "";
  $.each(cableData, function (i, item) {
    newContent += createButtons(".tableButton", 
          null, "submit", item.CommonCable);
  });

  $("#Categories").html(newContent);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM