选择表单内的元素在JQuery中不起作用 [英] Select Element inside Form not working in JQuery

查看:67
本文介绍了选择表单内的元素在JQuery中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML表单:

I have the following HTML form:

<div id="main">
  <form Id="search-form" action="/ViewRecord/AllRecord" method="post">
    <div>
        <fieldset>
            <legend>Search</legend>
           <p>
                <label for="username">Staff name</label>
                <input id="username" name="username" type="text" value="" />
                <label for="softype"> software type</label>

                <input type="submit" value="Search" />
            </p>
        </fieldset>
    </div>
  </form>
</div>

我想在单击username字段时显示一个alert框.这是我的JQuery代码:

And I want to show an alert box when the username field is clicked. This is my JQuery code:

$(function() {
$("#username").click(function() {
        $.getJSON("ViewRecord/GetSoftwareChoice", {},
    function(data) {
        alert(data);
    });
    });
});

由于某些原因,Click永远不会触发;我做错了什么吗?

For certain reason the Click is never fired; anything I did wrong?

推荐答案

您的代码正确,应该触发该事件.问题可能出在AJAX调用上.在ajax调用之前放置一个警报,以确保确实存在问题.

Your code is correct and the event should be fired. Probably the problem is with AJAX call. Put an alert before the ajax call to make sure that this is indeed the problem.

这篇关于选择表单内的元素在JQuery中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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