如何使用 AJAX/jQuery 将表单输入字段的值传递给脚本?
我当前的代码是这样的。
index.php
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<select id="dropdown_id">
<option value="one.php">One</option>
<option value="two.php">Two</option>
</select>
<div id="workspace">workspace</div>
one.php
$arr = array ( "workspace" => "One" );
echo json_encode( $arr );
two.php
$arr = array( 'workspace' => "Two" );
echo json_encode( $arr );
JS
jQuery('#dropdown_id').live('change', function(event) {
jQuery.getJSON($(this).val(), function(snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
jQuery('#' + id).html(snippets[id]);
}
});
});
上面的代码运行良好。当我从下拉列表中选择 One 时,one.php 将使用 JSON 加载到工作区 DIV 中。当我选择“二”时,two.php 将加载到工作区 DIV 中。
下一步我想要做什么:
Now I want to add another dropdown in index.php:
<select id="myinput" name="alpha">
<option value="a">a</option>
<option value="b">b</option>
<option value="a">c</option>
<option value="b">d</option>
</select>
当用户从第一个下拉列表中选择任何选项时,应该使用工作区 DIV 中第二个下拉列表的值加载该脚本(值)。
例如:
当我从第一个下拉列表中选择One时:
one.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'a'
echo json_encode( $arr );
现在当我从第二个下拉列表中选择b时下拉菜单:
one.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'b'
echo json_encode( $arr );
My current code is like this.
index.php
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<select id="dropdown_id">
<option value="one.php">One</option>
<option value="two.php">Two</option>
</select>
<div id="workspace">workspace</div>
one.php
$arr = array ( "workspace" => "One" );
echo json_encode( $arr );
two.php
$arr = array( 'workspace' => "Two" );
echo json_encode( $arr );
JS
jQuery('#dropdown_id').live('change', function(event) {
jQuery.getJSON($(this).val(), function(snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
jQuery('#' + id).html(snippets[id]);
}
});
});
Above code is working perfectly. When I select One from dropdown then one.php is loaded in workspace DIV using JSON. When I select Two then two.php is loaded in workspace DIV.
What I want to do next:
Now I want to add another dropdown in index.php:
<select id="myinput" name="alpha">
<option value="a">a</option>
<option value="b">b</option>
<option value="a">c</option>
<option value="b">d</option>
</select>
When user select any option from first dropdown then that script(value) should be loaded with the value of second dropdown in workspace DIV.
For example:
When I select One from first dropdown then:
one.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'a'
echo json_encode( $arr );
Now When I select b from second dropdown then:
one.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'b'
echo json_encode( $arr );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望它可以帮助你:
one.php
JS
I hope it can help you :
one.php
JS
怎么样:
然后你的 PHP 文件将查看 $_POST['input'] 值来输出该值。
How about something like:
Then your PHP files will look at the $_POST['input'] value to output the value.