douqin1932 2013-01-02 13:30
浏览 52
已采纳

Jquery - 选择表单中的项目并发布它们

I have a simple html page below calling a global.js file the global.js file calls a php file that gets a location for a name if it is in a database. (could have put it all on the one page but was following a tutorial).

 <!doctype html>
 <html
   <head>
<title>AJAX Database</title>
</head>
<body>

Name: <input type = "text" id="name">
<br />
<input type="submit" id="submit" value="Get Loc">


<div id="output"></div>
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="js/global.js"></script>
</body>
 </html>

global.js is :

$('#submit').on('click',function() {
var name = $('#name').val();
if($.trim(name) != ''){
   $.post('ajax/name.php', {name: name}, function(data) {
    $('div#output').text(data);
      });
}
});

it works fine as is, but if I put in tags as shown below, it won't work. I also want to use a fieldset, but I can't even get it to work with form tags.

I have used other selectors but it won't work. The problem seems to be the submit button, as it works if that is out of the form.. any ideas? I think using the submit within the form is getting the $.post function to send more than I want it too.

 <!doctype html>
 <html
 <head>
<title>AJAX Database</title>
</head>
<body>

    <form>
Name: <input type = "text" id="name">
<br />
<input type="submit" id="submit" value="Get Loc">
</form>

<div id="output"></div>
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="js/global.js"></script>
</body>
 </html>

the php file is:

 if(isset($_POST['name']) === true && empty($_POST['name']) === false) {
  require '../db/connect.php';

$query = mysql_query("
SELECT `names`.`location`
FROM    `names`
WHERE `names`.`name` = '" . mysql_real_escape_string(trim($_POST['name'])) . "'

");
echo (mysql_num_rows($query) !== 0) ? mysql_result($query,0,'location') : 'Name not found';

 }

Is my problem not using the right selectors, or is there some rule about using selectors for submit buttons within forms ?

  • 写回答

2条回答 默认 最新

  • duanraotun1674 2013-01-02 13:36
    关注

    You need to prevent the default action of the form. Either by using event.preventDefault() or adding return false at the end of the function.

    $('form').on('submit', function(e) {
        e.preventDefault();
        var name = $('#name').val();
        if ($.trim(name) != '') {
            $.post('ajax/name.php', {
                name: name
            }, function(data) {
                $('div#output').text(data);
            });
        }
    });​
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog