下面的代码创建了一个按钮,当单击其中一个按钮时它会使用 CSS 改变颜色,创建的每个按钮都有自己的值。 所以我想在我的 HTML 页面上单击时显示所选按钮的值,如何管理它?
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()
或 $(ths).attr('val')
将为您获取值。
关于javascript - 如何显示所选按钮的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241038/