dongpi0658 2016-05-04 12:18
浏览 55
已采纳

jquery不会通过调用ajax页面来更改输入值

Problem solved. Check working code on bottom.

first, sorry for my english. If possible pls rewrite my question.

I can not change input value with jquery from calling ajax page.

(jquery version http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js)

My main page is index.php and ajax page is ajax.php. I called ajax.php with

$.ajax(...

But after working ajax codes then input value didnt change on index.php.

here is index.php input code

<input name="guncel_fiyat" type="hidden" id="guncel_fiyat" value="old value">

here is index.php ajax calling code:

$.ajax(
            {
                type: "POST",
                url: "../ajax.php",
                data: post_edilecek_veri,
                cache: false,
                success: function(donen_veri)
                {
                    some code here;
                }
            }   

here is ajax.php code (i did try 3 different method but nothing change)

$(".guncel_fiyat").val("new value");

or

$("#guncel_fiyat").val("new value");

or

$(".guncel_fiyat input").val("new value");

after calling ajax then try get new value on index.php but only getting "old value" val.

var guncel_fiyat_degeri = $("#guncel_fiyat").val();
alert(guncel_fiyat);

HERE IS WORKING CODE

CALL_AJAX.HTML:

   <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
function peyfunc() 
{ 
    "use strict";
    var current_price = $("#current_price").val();
    alert ("current_price="+current_price);
    $.ajax(
            {
                url: "ajax_test.php",
                cache: false,
                success: function(html)
                {
                    $("#display").html(html);
                }
            }   
    );

}
</script>
</head>
<body>
<form id="form1" name="form1" method="post">
  <input name="current_price" type="hidden" id="current_price" value="5">
  <input type="button" name="button" id="button" value="Button"  onclick="peyfunc()">
</form>
<div id="display"></div>
</body>
</html>

AJAX_TEST.PHP

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
test
<script type="text/javascript">
//alert("aaa");
$("#current_price").val("new value");
alert ($("#current_price").val());
</script>
  • 写回答

1条回答 默认 最新

  • douzhong3887 2016-05-05 08:45
    关注

    HERE IS WORKING CODE

    CALL_AJAX.HTML:

       <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript">
    function peyfunc() 
    { 
        "use strict";
        var current_price = $("#current_price").val();
        alert ("current_price="+current_price);
        $.ajax(
                {
                    url: "ajax_test.php",
                    cache: false,
                    success: function(html)
                    {
                        $("#display").html(html);
                    }
                }   
        );
    
    }
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="post">
      <input name="current_price" type="hidden" id="current_price" value="5">
      <input type="button" name="button" id="button" value="Button"  onclick="peyfunc()">
    </form>
    <div id="display"></div>
    </body>
    </html>
    

    AJAX_TEST.PHP

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    test
    <script type="text/javascript">
    //alert("aaa");
    $("#current_price").val("new value");
    alert ($("#current_price").val());
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效