简体   繁体   中英

How to can i display a value of a selected button?

The following code creates a Buttons, with CSS when one of the button is clicked it changes color, each button created has its own value. So i want to display a VALUE of a selected button if clicked on my HTML page how can manage that?

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

function toggle(ths) {
  $(ths).toggleClass("btnColor");
   $("#tb").toggleClass("btnColorR");  
}

function DisplayButtons(cableData) {
  var newContent = '';
  $.each(cableData,
    function (i, item) {
      newContent += createButtons("tb" + item.CommonCable, null, "submit",       item.CommonCable,toggle);
  });
  $("#Categories").html(newContent);
}

$(ths).val() or $(ths).attr('val') will get you the value.

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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