添加jquery时JavaScript无法正常工作 [英] Javascript not working when jquery is added

查看:100
本文介绍了添加jquery时JavaScript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript n jquery的新手.我在脚本标签上使用了javascript和jquery.当不添加jquery时,javascript可以正常工作,但是当添加jquery函数时,脚本不起作用.....我应将两者都转换为javascript还是将两者都转换为jquery还是我错过了任何东西.这是我的剧本

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>

我在这里根据选定的城市加载位置.当onclick不存在时它可以正常工作,但是当添加时,位置不加载n功能未调用.需要做任何其他事情才能使两者都能正常工作....谢谢

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

推荐答案

您忘记了)

$(function () {

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

如果您正确缩进了代码块,并且在javascript控制台上查看,则这种错误将变得更容易被检测到.只需采用缩进样式并编写遵循该样式的代码即可.

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.

这篇关于添加jquery时JavaScript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆