在IF语句中执行jquery函数?

H.H*_*ORY 0 javascript jquery

我正在尝试在IF语句中执行jquery函数.

基本上,我得到一个选择选项的值,如果所选的选项的值是我想要的,(htl in this example)那么我想执行该功能!

但是当我在IF语句中包装代码时,我得到语法错误,我不知道是什么导致了这个问题.

这是我的整个代码:

var ascending = false;
$('.page_navigation .sortBy').change(function () {    
    var vals = $(this).val();
    if (vals == "htl") {    
        ///// I need to put the code bellow here    
    }
    var sorted = $('.mypro').sort(function (a, b) {
        return (ascending == (convertToNumber($(a).find('.prod-price').html()) < 
                  convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
    });
    ascending = ascending ? false : true;

    $('#myCont').html(sorted);
});
var convertToNumber = function (value) {
    return parseFloat(value.replace('£', ''));
}
Run Code Online (Sandbox Code Playgroud)

有人可以就这个问题提出建议吗?

我试过这个,我得到语法错误:

var ascending = false;

$('.page_navigation .sortBy').change(function(){

    var vals = $(this).val();
    if(vals == "htl") {


    var sorted = $('.mypro').sort(function(a,b){
        return (ascending ==
               (convertToNumber($(a).find('.prod-price').html()) < 
                convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
    });
    ascending = ascending ? false : true;

    $('#myCont').html(sorted);
});
var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
}
}
Run Code Online (Sandbox Code Playgroud)

Lia*_*iam 5

你关闭了如果错了.你需要在函数调用中放置右括号(if语句),所以:

var ascending = false;

$('.page_navigation .sortBy').change(function(){

    var vals = $(this).val();
    //closing brace matches this opening one
    if(vals == "htl") {    
          var sorted = $('.mypro').sort(function(a,b){
          return (ascending ==
               (convertToNumber($(a).find('.prod-price').html()) < 
                convertToNumber($(b).find('.prod-price').html()))) ? 1 : -1;
       });
       ascending = ascending ? false : true;

       $('#myCont').html(sorted);
   //put the brace inside the function...i.e. close the if brace
   }
});
var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
}
Run Code Online (Sandbox Code Playgroud)

不正确的位是:

var convertToNumber = function(value){
     return parseFloat(value.replace('£',''));
//WHY's this brace here!
}
}
Run Code Online (Sandbox Code Playgroud)