开发者

javaScript jQuery function, how do i pass variables

开发者 https://www.devze.com 2023-02-24 06:08 出处:网络
What is wrong with my code. How do i pass attrfull to the 开发者_运维知识库inside. The way i have it done, if i run the function editsubmit($selected, size), $selected is inserted properly but i\'m ge

What is wrong with my code. How do i pass attrfull to the 开发者_运维知识库inside. The way i have it done, if i run the function editsubmit($selected, size), $selected is inserted properly but i'm getting attrfull instead of size.

function editsubmit(attr, attrfull) {
    if (attr.length) {
        attr.val().length ? $selectedinput.attr({
            attrfull: attr.val()
        }) : $selectedinput.removeAttr(attrfull);
    }
}

$selected is a variable and attrfull i a string. Do i need double qoutes around the string when i run the function like editsubmit($selected,'size').


Try

function editsubmit(attr, attrfull) {
    if (attr.length) {
        attr.val().length ? $selectedinput.attr(attrfull, attr.val()) : $selectedinput.removeAttr(attrfull);
    }
}


Yes, you do need it to be a string (in double quotes), or else it will think you're trying to pass a variable reference.


The problem is this: {attrfull: attr.val()}

I think you want it to be {size: (whatever attr.val() is)}

So:

function editsubmit(attr, attrfull) {
    if (attr.length) {
        if (attr.val().length) {
          var myObj = {};
          myObj[attrfull] = attr.val();
          $selectedinput.attr(myObj);
        } else {
          $selectedinput.removeAttr(attrfull);
        }
    }
}
0

精彩评论

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

关注公众号