开发者

Javascript, add string if true

I need to modify this function:

开发者_运维问答function checkPermissions(fid, obj) {

$("#permissions_"+fid+"_canview").attr("checked", obj.checked);

}

if the param "n" is "true" then instead of #permissions it'll output #permissionsSet.

is that possible?


OP clarified the question to be change this whether or not a 3rd parameter was provided. Here you go.

function checkPermissions(fid, obj, n) {
  var id = n !== undefined ? '#permissionsSet' : '#permissions';
  $(id + fid + "_canview").attr("checked", obj.checked);
}

Note: This function can be freely used with passing 2 or 3 parameters (or really any number).

checkPermissions(1, this);        // Ok n === undefined 
checkPermissions(1, this, true);  // Ok n === true
checkPermissions(1);              // Okish n and obj === undefined


function checkPermissions(fid, obj, n) {

    $("#permissions" + ((n) ? "Set" : "") + "_"+fid+"_canview").attr("checked", obj.checked);

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜