doufu6504 2017-03-10 08:27
浏览 80
已采纳

是否可以在我的表单提交按钮上使用onClick功能?

I store multiple data in a table for enquiry basis.. which is successfully stored in my table. but now i also want to store those same data in Booking basis.

So i used onClick function for storing enquiry data. and now i want to store booking data in table from form submit button.

here is my form submit button code:

<button type="submit" class="booking-btn" onclick="btn_booking_hour();" id="btn_booking_hour">Book Now</button>

Here is my form action:

<?php echo form_open('booking/'.$product->id); ?>

And here my function:

function btn_booking_hour()
    { 

    var date = $('#datehour').val();
    var renter_id = $("#ownerid").val();
    var guests_quantity = $('#no_of_guests').val();
    var property_id = $('#property_id').val(); 
    var selectedIds="";
    var selectedObject = document.getElementsByClassName('hour_slots_available slot_activated');
    for(var i=0;i<selectedObject.length;i++)
      selectedIds+=selectedObject[i].id+",";

    if(selectedIds == ""){

        alert("Please select Hour-Slot(s) before Booking");
    }
    else{

        $.ajax({

    type: 'POST',
    dataType:'json',
    url:baseURL+'site/rentals/ajaxhourshow',

    data:{'selectedHours':selectedIds,'property_id':property_id,'date':date,'guests_quantity':guests_quantity,'renter_id':renter_id},

    success: function(responce){

        var myJSON = JSON.stringify(responce);
        var packet = $.parseJSON(myJSON);

        var  prd_id=packet.property_id;
        var hour_slots=packet.hour_slots;
        window.location.href = baseURL+'booking/'+prd_id;
        }
    });
   }

}

Please suggest me how can i submit a form with data within their onClick function. Thanks..

  • 写回答

2条回答 默认 最新

  • duanluanlang8501 2017-03-10 08:32
    关注

    Not sure if you are using a form, if you are using it you can can use onsubmit handler and return the action. In that case you wont need onclick handler in the button

    <form onsubmit = "return btn_booking_hour()">
      // Rest of code
    </form>
    

    You can also do like this

    $('#form').on('submit',function(e){
     e.preventDefault();
     // rest of the code
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办