如何将每个功能或方法分配给按钮? [英] How can i assign each function or method to a button?

查看:68
本文介绍了如何将每个功能或方法分配给按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript页面,当页面加载时会创建切换按钮,但我现在不能单独使用这些按钮,因为按钮的id是我数据库表上的列名。所以我事先无法知道,这就是为什么我不能在每个按钮上分配方法或功能。



这是切换按钮的创建方式:



  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 + < span class =code-string>' \''' ')+ ' >';

}

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.C ommonCable,
null submit,item.CommonCable,toggle());
});
$( #Categories
.html(newContent);
}



注意:任何可以帮助 asap 的人,我如何为每个按钮分配每个功能或方法? :或者我可以使用任何其他方式来创建我的切换按钮?



[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

.each(cableData, function (i,item){
function toggle(){


#tb + item.CommonCable)
.clicked?


' #MyElement'
.addClass(' clickedButton');

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. 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 ?

[edit]Code block added - OriginalGriff[/edit]

解决方案

.each(cableData, function (i, item) { function toggle() {


("#tb" + item.CommonCable) .clicked ?


('#MyElement') .addClass('clickedButton');


这篇关于如何将每个功能或方法分配给按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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