0

我正在 asp.net 中开发一个讨论面板,其中我使用带有两个类的 jquery 选择器选项

 $("#plblDisAgreeProblem", "plblDisAgreeComment").click(function(){
var str = {
            problemID: $("#problemID").val(),
            empID : $("#empID").val(),
            commentID : -1
        }
        $.ajax({

            type: "GET",
            url: "<%= Url.Action("GetStatus", "Discussion") %>",
            data: str,
            error: function(msg){
                    alert("error2" + msg);
                },
                success: function (msg) {

                  var out = msg.split("<br/>");
                  if (out[1] == "DisAgree: True")
                  {
                        alert("You are already disagree to this problem.");
                  }
                  else
                  {




                }
            });


        })

我想如果 div 的类是“plblDisAgreeProblem”,那么在 JSon 中的 commentID 应该是 -1,如果 div 的类是“plblDisAgreeComment”,那么在 JSon 中的 commentID 应该是 this.id,但我该怎么做呢?

请帮我

4

4 回答 4

1

使用这个 is() 函数:

commentID : $(this).is(".plblDisAgreeProblem") ? -1 : this.id

我相信这应该可行(无法从我现在所在的位置进行测试)。

于 2012-04-16T17:41:15.747 回答
1

首先让我们修复类选择器中缺少的点.. 见下文,

$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function(){

然后您可以使用 hasClass 函数检查单击的元素是否来自指定的类..见下文,

    var str = {
        problemID: $("#problemID").val(),
        empID : $("#empID").val(),
        commentID : -1
    }

   if ($(this).hasClass('plblDisAgreeComment') {
       str.commentID = this.id;
   } 

我们不需要,else if of ('plblDisAgreeProblem')因为它默认为 -1。

于 2012-04-16T17:41:33.450 回答
0
$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function(e) {
  ....
  ....

  if(this.className == 'plblDisAgreeComment') {
     str.commentID = this.id;
  } else {
   // do other
  }
  ......
  ......
});

类选择器缺少点 (.)

于 2012-04-16T17:36:27.070 回答
0

这应该可以解决问题:(也修复了几个语法错误)

$("#plblDisAgreeProblem", ".plblDisAgreeComment").click(function() {
    var str = {
        problemID: $("#problemID").val(),
        empID: $("#empID").val(),
        commentID: $(this).hasClass('plblDisAgreeComment') ? this.id : -1
    };
    $.ajax({
        type: "GET",
        url: "<%= Url.Action("GetStatus", "Discussion") %>",
        data: str,
        error: function(msg) {
            alert("error2" + msg);
        },
        success: function(msg) {
            var out = msg.split("<br/>");
            if (out[1] == "DisAgree: True") {
                alert("You are already disagree to this problem.");
            } else {}
        });
    });
});

速度参考:

jQuery .hasClass() 与 .is()

于 2012-04-16T19:18:18.017 回答