开发者

Ruby on Rails: jQuery why is val not defined?

开发者 https://www.devze.com 2023-01-10 11:12 出处:网络
function replaceIfEmpty(fieldID, value){ alert($j(\'input#\'+fieldID.val())); if ($j(\'input#\'+fieldID).val() == \"\"){
function replaceIfEmpty(fieldID, value){
    alert($j('input#'+fieldID.val()));
    if ($j('input#'+fieldID).val() == ""){
        $j('input#'+fieldID).val(value);
    }
}

there's my function, and this is in my c开发者_StackOverflowontroller:

      page << "replaceIfEmpty('object_name', '#{t.name}');"

but when all this is invoked, an alert tells me:

RJS error:

TypeError: Object object_name has no method 'val'

even though I'm using jQuery 1.3.2, the docs say .val() isn't new to 1.4 =\


Your parenthesis are a bit off, this:

alert($j('input#'+fieldID.val()));

Should be:

alert($j('input#'+fieldID).val());

Currently you're trying to call .val() on the string fieldID, rather than the jQuery object.


A bit of a tangent from the issue here: If you upgrade to 1.4+ you can make this a bit shorter by passing a function to .val(), like this:

function replaceIfEmpty(fieldID, value){
  $j('#'+fieldID).val(function(i, oldVal) {
    alert(oldVal);
    return oldVal == "" ? value : oldValue;
  });
}


Based on your error, it looks to me as though fieldID is not what you think it is. Have you debugged using Firebug at all?

0

精彩评论

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