jQuery在数据网格中更少的按钮,在更多点击但不能更少的情况下工作

时间:2012-06-11 14:26:57

标签: c# jquery gridview datagrid

我有一个按钮,在搜索结果中显示或多或少的内容块。当我点击“更多”时,它会根据需要展开,但是当我点击“减去”时没有任何反应。我觉得这与我的preventDefault(e)行有关。代码如下......

$(".btnMore").click(function (e) {
    e.preventDefault(e);
    if ($(this).attr('value', 'More')) {
        $(this).parent().prev().switchClass("abstractText", "abstractTextNoHeight", 500, "swing");
        $(this).attr('value', 'Less');
    }
    else if ($(this).attr('value', 'Less')) {
        $(this).parent().prev().switchClass("abstractTextNoHeight", "abstractText", 500, "swing");
        $(this).attr('value', 'More');
    }

});

3 个答案:

答案 0 :(得分:2)

这是使用“attr()”检查属性值的错误方法:

if($(this).attr("value", "More"))

将“value”属性的值设置为“More”并返回undefined。你需要做的是:

if($(this).attr("value") == "More")

if(this.getAttribute("value") == "More")

当JavaScript足够时,不需要使用jQuery。注意$()周围缺少this - 因为你正在使用JS函数(getAttribute),所以你不需要创建一个jQuery集合,这使你的代码更快更简单

答案 1 :(得分:0)

试试这个:

$(".btnMore").click(function (e) {
e.preventDefault(e);
if ($(this).attr('value')=='More') {
    $(this).parent().prev().switchClass("abstractText", "abstractTextNoHeight", 500, "swing");
    $(this).attr('value', 'Less');
}
else if ($(this).attr('value')=='Less') {
    $(this).parent().prev().switchClass("abstractTextNoHeight", "abstractText", 500, "swing");
    $(this).attr('value', 'More');
}

});

答案 2 :(得分:0)

结束这样做:

//more/less button
$(".btnMoreText a").click(function (e) {
    e.preventDefault();
    rowClicked = false;
    $(this).parent().parent().prev().switchClass("abstractText", "abstractTextNoHeight", 500, "swing");
    $(this).parent().addClass("hidden");
    $(this).parent().next().removeClass("hidden");
});

$(".btnLessText a").click(function (e) {
    e.preventDefault();
    rowClicked = false;
    console.log($(this).parent().parent().prev());
    $(this).parent().parent().prev().switchClass("abstractTextNoHeight", "abstractText", 500, "swing");
    $(this).parent().addClass("hidden");
    $(this).parent().prev().removeClass("hidden");
});