Yii, attaching a javascript function on submit button

做~自己de王妃 提交于 2019-12-24 15:42:46

问题


I want to attach a function to my submit button. But my code doesn't seem to be calling the function.

Basically what I want it to do is, enabling all disabled field when the form is being submitted.

This below is my code:

<script>
function enableField() {
    $("#Booking_clientPackagedService_id").prop("disabled", false);
}
</script>


<?php
    echo CHtml::submitButton(Yii::t('app', 'Save'));
    $this->endWidget();
?>

This #Booking_clientPackagedService_id initially is disabled after certain action being done. But when I click on submit, I would like to enable the field. How can I do it? Please advise, thanks! :D

EDIT

So I've removed onclick event on my submit button and added as per Kancho Iliev's comment.

$( "#booking-form" ).submit(function() {
  $("#Booking_clientPackagedService_id").prop("disabled", false);
});

But it is still not working. Where have I missed out?


回答1:


Remove onclick event, and add

$("your_form_name").submit(function(){
   enableField();
}); 



回答2:


This is the correct way of attaching a submit function:

$(function() {
    $("#formName").submit(function() {
        alert("hi");
        //do whatever 
    });
});


来源:https://stackoverflow.com/questions/29789946/yii-attaching-a-javascript-function-on-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!