在Jquery对话框弹出窗口中填充表单元素

Populating form elements in a Jquery Dialog Popout

本文关键字:填充 表单 元素 窗口 Jquery 对话框      更新时间:2023-09-26

我是Jquery的新手,或者至少是Jquery更高级的功能的新手。几天来,我一直在断断续续地想办法解决这个问题,但我开始感到慌乱。

我有一个页面,我们使用HTML只读文本框来显示结果。php通过sql搜索填充文本框。然而,对于这个例子,我已经预设了PHP变量来直接解决这个问题。

我正在尝试基于php变量填充对话框中的表单字段。我已经精简了我的测试代码,希望它能自我解释。这是主页的代码:

<?php
$Test1 = 'Test';
$Test2 = 'TestTest';
$Test3 = 'TestTestTest';
$Test4 = 'TestTestTestTest';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/jquery-ui-1.8.13.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>
<title>Untitled Document</title>
<script>
  $(document).ready(function() {
    $('#popout1 a').each(function() {
        var $link = $(this);
        var $dialog = $('<div></div>') 
            .load($link.attr('href') + '#formTest' )
            .dialog({
                autoOpen: false,
                title: $link.attr('title'),
                width: 600,
                height: 400,
                modal: true,
        });
$link.click(function() {
    $dialog.dialog('open');
    $dialog.parent().appendTo($("#formTest"));
    return false;
    });
    });
}); 
</script>
</head>
<body>
<div id="formTest">
<form name="formTest" id="formTest">
<table>
    <th>Test</th>
        <tr>
            <td><label for="Test1">Test1:</label></td>
            <td><input type="text" readonly="readonly" name="Test1" value="<?php echo $Test1?>" /></td>
        </tr>       
        <tr>
            <td><label for="Test2">Test2:</label></td>      
            <td><input type="text" readonly="readonly" name="Test2" value="<?php echo $Test2?>" /></td>
        </tr>
        <tr>
        <td id="popout1"><a href="popoutTest.php" title="Popout Test">Click here for more data</a></td>
        </tr>
</table>
</form>
</div>
</body>
</html>

这是弹出页面的代码:

    <table>
    <th>More Tests</th>
        <tr>
            <td><label for="Test3">Test3:</label></td>
            <td><input type="text" readonly name="Test3" value="<?php echo $Test3 ?>" /></td>
        </tr>       
        <tr>
            <td><label for="Test4">Test4:</td>      
            <td><input type="text" readonly name="Test4" value="<?php echo $Test4 ?>" /></td>
        </tr>
</table>

我的问题是,如何使用主页面上声明的php变量在弹出页面上填充test3和test4文本框。

将输入的名称属性更改为id。

<table>
    <th>More Tests</th>
        <tr>
            <td><label for="Test3">Test3:</label></td>
            <td><input type="text" readonly id="Test3" value="<?php echo $Test3 ?>" /></td>
        </tr>       
        <tr>
            <td><label for="Test4">Test4:</td>      
            <td><input type="text" readonly id="Test4" value="<?php echo $Test4 ?>" /></td>
        </tr>
</table>

然后你可以用jQuery找到它们。

$('#Test3').val('the new value');
$('#Test4').val('the other new value');

编辑:您需要将php值保存在主页面的某个位置,以便在弹出窗口显示时可以使用。