2016-09-23 5 views
0

i'have в список списке опций содержит OnChange = "setLocation (this.value)"отменить OnChange событие через JQuery

<div class="sort-by"> 
    <select onchange="setLocation(this.value)"> 
     <option value="http://example.com/test.html?dir=asc&amp;order=position">Position</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=name">Name</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=price">Price</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=created_at" selected="selected">Date</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=end_date">End date</option> 
     <option value="Range">Range</option> 
    </select> 
</div> 

У меня есть JQuery потом добавит некоторую ценность

selectValues = { "Range": "Range" }; 
jQuery.each(selectValues, function(key, value) { 
    jQuery('.sort-by select').append(jQuery("<option></option>").attr("value", key).text(value)); 
}); 

Когда пользователь выбирает специфично Я хочу, чтобы отменить setLocation и сделать еще одну операцию

jQuery(document).ready(function(){ 
    jQuery('.sort-by select').change(function(){ 
     var selectedVal = jQuery(".sort-by select option:selected").val(); 

     if (selectedVal == "Range") { 
      return false;  
     } 
    }); 
}); 

Как я могу d o что?

+0

что .sort -выбор, это тот же самый вход для выбора, что и в que stion – atul

+1

один из способов сделать это - удалить атрибут onchange и сделать if, если в функции изменения jQuery – madalinivascu

+0

, если вы прикрепляете событие изменения через jquery, то почему вы добавляете атрибут onchange в свой элемент, вызывайте этот метод внутри своего jquery change event на основе вашего состояния – atul

ответ

0

Используйте removeAttr и удалите событие onClick.

$('select').removeAttr('onchange');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select onchange="setLocation(this.value)"> 
 
    <option value="http://example.com/test.html?dir=asc&amp;order=position">Position</option> 
 
    <option value="http://example.com/test.html?dir=asc&amp;order=name">Name</option> 
 
    <option value="http://example.com/test.html?dir=asc&amp;order=price">Price</option> 
 
    <option value="http://example.com/test.html?dir=asc&amp;order=created_at" selected="selected">Date</option> 
 
    <option value="http://example.com/test.html?dir=asc&amp;order=end_date">End date</option> 
 
    <option value="Range">Range</option> 
 
</select>

0

Удалите OnChange атрибут

<div class="sort-by"> 
    <select> 
     <option value="http://example.com/test.html?dir=asc&amp;order=position">Position</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=name">Name</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=price">Price</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=created_at" selected="selected">Date</option> 
     <option value="http://example.com/test.html?dir=asc&amp;order=end_date">End date</option> 
     <option value="Range">Range</option> 
    </select> 
</div> 

Изменить если заявление

jQuery(document).ready(function(){ 
    jQuery('.sort-by select').change(function(){ 
     var selectedVal = jQuery(".sort-by select option:selected").val(); 

     if (selectedVal != "Range") { 
      setLocation(selectedVal); 
     } 
    }); 
}); 
0

Вы можете очистить атрибут и отвязать событие

Jquery предварительно 1,7

$('select').attr('onchange','').unbind('onchange'); 

Jquery опубликовать 1,7

$('select').prop('onchange','').unbind('onchange'); 

Или вы можете сделать removeAttr также зависит от вас способом исполнения

jQuery(document).ready(function(){ 
jQuery('.sort-by select').change(function(){ 
    var selectedVal = jQuery(".sort-by select option:selected").val(); 

    if (selectedVal == "Range") { 
     return false;  
    } 
    $(this).attr('onchange','').unbind('onchange'); // or .prop() 
    }); 
}); 
+0

На самом деле это не отвечает на вопрос OP. – Samundra

Смежные вопросы