Ajax表单设置文本框值onsubmit

时间:2015-07-24 05:17:12

标签: javascript php jquery ajax forms

使用ajax提交表单后我无法正确设置多个文本框上的值,这是我的表单

<form id="searchForm" action="process.php" method="post">
<input id="cust_code" name="cust_code" required>
<input id="cust_code1" name="cust_code1" disabled>
<input id="cust_name" name="cust_name" disabled>

<button type="submit">Search</button>
</form>

process.php

$cust_code = $_POST['cust_code'];
$result = mysql_query("SELECT code,cust_name FROM information WHERE code=$cust_code") or die(mysql_error());
$data = mysql_fetch_array($result);

的javascript:

var formData = {
            'cust_code': $('#cust_code').val()
        };
        $.ajax({
            type        : 'POST',
            url         : 'process.php',
            data        : formData,
            dataType    : 'json',
            encode          : true,
            success: function(data)          // recieved data from process.php
            {
                var code = data[0];              
                var cust_name = data[1];           

                // Update id value
                document.getElementById('cust_code').value = code;
                document.getElementById('cust_code1').value = code;
                document.getElementById('cust_name').value = cust_name;


            }
        })

cust_code和cust_code1的值在提交后更改,但不在cust_name

中更改

任何想法?

编辑:id已经在另一个页面上使用(php include),这样输入就不会改变,解决了!

2 个答案:

答案 0 :(得分:0)

您也可以通过这种方式在JQuery中分配值:

document.getElementById('cust_code').val(code);

document.getElementById('cust_code').attr('value',code);

答案 1 :(得分:0)

在另一个页面上使用了相同的ID(php include),因此输入值不会改变,将ID更改为cust_names似乎可以解决问题

相关问题