开发者

jquery add row on click only if it's empty?

The code below works, insomuch that if I click in an input field, it'll add another row. I'm trying to figure out though, how to do that only if the input field is empty?

$("#tableSearchData > tbody > tr > td > input").bind('focus', function(){
                var row = $(this).closest("tr").get(0);
                if( row.className.indexOf("开发者_运维问答clicked")==-1 )
                {
                    var rowCopy=$(row).clone(true);
                                $(row).closest("tbody").append(rowCopy);
                                row.className+=" clicked";
                }
});


$("#tableSearchData > tbody > tr > td > input").bind('focus', function(){
      if ($(this).val() == "")
      {
                var row = $(this).closest("tr").get(0);
                if( row.className.indexOf("clicked")==-1 )
                {
                    var rowCopy=$(row).clone(true);
                                $(row).closest("tbody").append(rowCopy);
                                row.className+=" clicked";
                }
      }
});


If you just want to make sure there are no characters in the text box you can check .val() for ""

if ($(this).val() === "")

If you want to make sure that characters inside the textbox are not white spaces then you can trim the val and check for empty

if ($.trim($(this).val()) === "")

If your classname is exactly "clicked" then you can use .hasClass() to check whether the class is present on an element or not.

if (row.hasClass("clicked"))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜