duansen6750 2010-07-26 15:59
浏览 27
已采纳

第一次尝试ajax,脚本没被看见?

I've been coding with php/CI for a bit now and am starting to get the hang of it, but I'm trying to make the leap to use jQuery + some ajax stuff and I can't seem to get it to work. It's like the the script isn't even being seen.

What I'd like it to do is to call the java/test function when the user clicks the submit button. I'd like to add something like if(ajax) do something, which is why I have that in the form data, but no matte what it calls the submit function - I added the return false so it wouldn't fire the submit function, yet it still is.

Here is my code, any help would be great. Thanks

java.php // Controller

<?php

class Java extends Controller{

    function index(){
        $this->load->view('java_view');
    }

    function submit(){
        $one = $this->input->post('one');
        $two = $this->input->post('two');

        echo $one;
    }
            function test(){
        $one = $this->input->post('one');
        $two = $this->input->post('two');

        echo $two;
    }


}   

java_view.php // viewfile

<html>
<header>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="javascript">
    $(document).ready(function(){
        $('#submit').click(function(){

            var form_data = {
                one: ('#one').val(),
                two: ('#two').val(),
                ajax: '1',
            };

            $.ajax({
            url: "<?php echo site_url('java/test'); ?>",
            type: 'POST',
            data: form_data,
            success: function() {
                alert('success');
            }
            });
                            return false;
        }); 
    });
</script>

 </header>

 <body>

 <?php

echo form_open('java/submit');  

//setting up the fields
$form_data = array(
    'name' => 'one',
    'id' => 'one',
);

echo "<p> One </p> <br/>";

echo form_input($form_data);

$form_data = array(
    'name' => 'two',
    'id' => 'two',
);

echo "<br/> <p>Two</p> <br/>";

echo form_input($form_data);

$form_data = array(
    'name' => 'submit',
    'id' => 'submit',
    'value' => 'Submit',
);
echo "<br/>";

echo form_submit($form_data);
echo form_close();

  • 写回答

3条回答 默认 最新

  • douyi8732 2010-07-26 16:01
    关注

    This line:

    $('#submit').onclick(function(){
    

    Should be:

    $('#submit').click(function(){
    

    I assume that #submit isn't input type="submit" otherwise page will reload unless you put return false at the end of click handler.

    More Info:

    http://api.jquery.com/click/

    Update:

    Try this (added live and few other modifications):

     $(document).ready(function(){
        $('#submit').live('click', function(){
    
            var form_data = {
                one: ('#one').val(),
                two: ('#two').val(),
                ajax: '1',
            };
    
            $.ajax({
            url: "<?php echo site_url('java/test'); ?>",
            type: 'POST',
            data: form_data,
            success: function() {
                alert('success');
            });
    
            return false;
        }); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测