0

外部jsをロードするページがあり、そのページに関数がありますcalled calculate_totals()。私が欲しいのはcalculate_totals()、外部jsの関数から関数を呼び出すことです。

これは、外部jsに読み込まれるJavaScriptの一部です

function selectItemMouse(thisVar)
{   
    var searchValue = thisVar.attr('data-name');
    thisVar.parent().parent().parent().parent().children('.list').val(searchValue);
    
    
    //alert(thisVar.parent().parent().parent().parent().next().find('input').focus());
    
    thisVar.children().children().each(function()
    {
        var dataId = $(this).attr('data-id');
        var value = $(this).attr('data-name');
        
        //This change the input of my form
        $(thisVar).parent().parent().parent().parent().children().children('.'+dataId).val(value);
    });
    
    $('.customerResults').hide();
    calculate_totals();     
}
4

2 に答える 2

1

入力に変更のリスナーを配置できます。

$(document).ready(function(){
    $('#input').change(function(){
        alert("alert here");
    });
});
于 2013-01-15T11:29:16.100 に答える
1

コメントセッションに基づく: calculate_totals は内部で定義されています$(document).ready(function(){});

そのため外には見えません。基本的には中 $(document).ready(function(){});のみご覧いただけます。それを外側に移動するだけです。今ではグローバルになり、どこでも見えるようになります。

それ以外の

$(document).ready(function(){
   function calculate_totals() {} // this function is visible inside of a ready function only
  ///..... other code here
});

使用する:

function calculate_totals() {} // now it is a part of global context
$(document).ready(function(){
  ///..... other code here   
});

その後、selectItemMouse 内およびその他の場所で使用できるようにする必要があります。

その上、私はあなたのコードを更新することを考えます。

thisVar.parent().parent().parent().parent().children('.list').val(searchValue);

このようなチェーンの使用は柔軟ではありません。thisVar 要素を他の要素でラップするだけで、JS コードを更新する必要があります。

.parentsまたはをご覧ください.closest。たとえば、上記の行を次のように変更できます。

thisVar.closest(".class_common_parent").children('.list').val(searchValue);

.class_common_parent使用する要素のクラスはどこですかparent().parent().parent().parent().

于 2013-01-15T12:08:08.660 に答える