isFunction为什么不能用于隐藏/显示?

http://jsbin.com/tavirasi/1/edit?js,output

$('.form select[data-change]').change(function(){
    var $el = $($(this).data('change'));
    if($el.length){
        var action = $(this).find(':selected').data('action');
        if($.isFunction(action)){
            $el[action]();
        }
    }
}).change();


的HTML

<form action="" class="form">

    <select name="question" data-change="input[name=customer_question]">
      <option data-action="hide">Mothers maiden name</option>
      <option data-action="foo">Prevent error</option>
      <option data-action="show">Custom question</option>
    </select>

  <input type="text" name="customer_question" style="display:none" />

  </form>

最佳答案

您传递的字符串当然不是函数。它应该是:

if ( $.isFunction( $el[action] ) )

关于jquery - jQuery在调用之前检查字符串是否作为函数存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25079787/

10-12 12:51
查看更多