Javascript not working when jquery is added

拜拜、爱过 提交于 2019-12-13 09:34:41

问题


I am new to javascript n jquery. I used javascript along with jquery on my script tag.When jquery is not added, the javascript works fine,but when a jquery function is added, the script is not working.....shall i convert both to javascript or both to jquery or am i missing anything.Here is my script

<script type="text/javascript">
function getLocations() {
    $.post('@Url.Action("getLocations","Home")', { 'id': $("#cities").val() },
        function (data) {
            $("#loca").html(data).show();

        });
}

$(function () {
$('.submit').on('click', function () {
    var ck = "";
    var city = $("#cities option:selected").text();
    var location = $("#loca option:selected").text();
    alert(city+"and"+location)
}
});

</script>

here i am loading location based on the city selected.Its works fine when the onclick is not there,But when added ,location are not loading n the function is not calling.I have tried by butting alert inside it.Do i need do any thing else for both to work....Thank You


回答1:


you forgot a )

$(function () {

    $('.submit').on('click', function () {
       ...
    })   // <---
});

if you properly indent the code blocks and if you look on the javascript console, this kind of errors become easier to be detected. Just adopt an indent style and write code adhering to it.



来源:https://stackoverflow.com/questions/21993692/javascript-not-working-when-jquery-is-added

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