KnockoutJS: click event invoked on every Option in Select

KnockoutJS: click event invoked on every Option in Select

Asked on December 25, 2018 in knockout js.
Add Comment


  • 2 Answer(s)

        This are some suggestion to find the solution :

        Try to use the  change binding in the place of click and optionsText binding in the place of option tag and use function in change binding in the place of just calling alert:

    <select data-bind="options: availableMeals, optionsText: 'mealName', value: selectedMeal, event: {change: onChange}">
    </select>
    function Meal(name, price){
        var self = this;
        self.mealName = name;
        self.price = price;
    }
    function ReservationsViewModel() {
        var self = this;
        self.availableMeals = ko.observableArray(
            [new Meal("Standard (sandwich)", 0),
            new Meal("Premium (lobster)", 34.95),
            new Meal("Ultimate (whole zebra)", 290)]);
        self.selectedMeal = ko.observable(self.availableMeals()[0]);
        self.onChange = function() {
            alert("Hello");
        };
    }
    ko.applyBindings(new ReservationsViewModel());
    

     

    Answered on December 25, 2018.
    Add Comment

        Try this on alert .

        Buried the “alert”  in a function:

    <select data-bind="foreach: availableMeals, event: {change: function () { alert('hello'); } }">
        <option data-bind="text: mealName " />
    </select>
    
    Answered on December 25, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.