只提交第一次knockout.js和bootstrap模态

时间:2014-01-15 15:22:21

标签: javascript html twitter-bootstrap knockout.js bootstrap-modal

我有问题,有knockout.js和bootstrap模态。在按钮上单击我打开bootastrap模式,用于创建新城市。然后我提交表格并保存在基地。问题是因为提交仅适用于第一次。之后它不起作用。 这是它的样子

html文件

> <!-- Modal add new city
>      ==================================================-->
>     <div class="modal fade" id="addCity" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
>       <div class="modal-dialog">
>         <form data-bind="submit: onSubmit">
>         <div class="modal-content">
>           <div class="modal-header">
>             <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
>             <h4 class="modal-title">Dodaj novi grad</h4>
>           </div>
>           <div class="modal-body">
>             <div class="input-group">
>               <span class="input-group-addon">Ime grada</span>
>               <input type="text" class="form-control" placeholder="Ime grada" data-bind="value : city_name">
>           </div
>     
>     
>         </div>
>           <div class="modal-footer">
>             <button type="button" class="btn btn-default" data-dismiss="modal">Zatvori</button>
>             <button type="submit" class="btn btn-success">Sacuvaj</button>
>           </div>
>         </div><!-- /.modal-content -->
>         </form>
>       </div><!-- /.modal-dialog -->
>     </div><!-- /.modal -->

js file

    var viewModel = ko.computed(function()
{
    var self = this;

        var cities = getAllCities();
    self.cities = ko.observableArray(cities);



    self.city_name = ko.observable();
    self.logo_path = ko.observable();

    //add new city
    self.onSubmit = function() 
    {

        var data = JSON.stringify(
            {
                city_name : self.city_name(), logo_path : self.logo_path()        
            }); // prepare request data
        var response =  ajaxJsonPost("/services/private_service/add_city", data, false, false);
        if (response.status === ResponseStatus.SUCCESS) {
        $("#cityName").attr("value", "");
        $('#addCity').modal('hide');
        self.cities.push(data);
        } else {
            //handle error
        }
    };

});

ko.applyBindings(new viewModel());

1 个答案:

答案 0 :(得分:0)

你能不只是做一个ajax请求而不是表单提交?类似的东西:

$.ajax({
    url: "/services/private_service/add_city",
    data: data,
    success: function (data) {
        $("#cityName").attr("value", "");
        $('#addCity').modal('hide');
        self.cities.push(data);
    }
});

除非你有这个方法的书面脚本,否则我不熟悉“ajaxJsonPost()”。