[英]How to pass values of form input fields to a script using AJAX/jQuery?
My current code is like this. 我当前的代码是这样的。
index.php 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 一个.php
$arr = array ( "workspace" => "One" );
echo json_encode( $arr );
two.php two.php
$arr = array( 'workspace' => "Two" );
echo json_encode( $arr );
JS 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.
当我从下拉列表中选择一个时,one.php将使用JSON加载到工作区DIV中。 When I select Two then two.php is loaded in workspace DIV.
当我选择Two时,two.php将加载到工作区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. 当用户从第一个下拉菜单中选择任何选项时,该脚本(值)应在工作区DIV中加载第二个下拉菜单的值。
For example: 例如:
When I select One from first dropdown then: 当我从第一个下拉菜单中选择一个时 :
one.php 一个.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'a'
echo json_encode( $arr );
Now When I select b from second dropdown then: 现在,当我从第二个下拉菜单中选择b时 :
one.php 一个.php
$arr = array ( "workspace" => $_GET['alpha'] ); // Should load 'b'
echo json_encode( $arr );
I hope it can help you : 希望它能对您有所帮助:
one.php 一个.php
$arr = array ( 'workspace' => 'One');
if(isset($_GET['myinput_val']))
{
$arr['workspace'] = $_GET['myinput_val'];
}
echo json_encode( $arr );
JS JS
$('#myinput').live('change', function(event) {
$.getJSON("one.php", { myinput_val: $(this).val() } , function (snippets) {
for(var id in snippets) {
// updated to deal with any type of HTML
$('#' + id).html(snippets[id]);
}
});
});
How about something like: 怎么样:
$('#mypage').bind('change', function () {
var self = $(this);
$('#workspace').load('/' + self.val(), {
input: $('#myinput').val();
});
});
Then your PHP files will look at the $_POST['input'] value to output the value. 然后,您的PHP文件将查看$ _POST ['input']值以输出该值。
<?php echo $_POST['input']; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.