jquery’变量’到php’变量’
我在这里遇到同样的问题,但我需要的是获取所选选项的值然后将其保存在php变量而不是文本框中。 我差不多花了两天时间寻找我的金币。 任何帮助都会。谢谢。
编辑这是我编写代码的方式。
$(document).ready(function () { $("#country").change( function () { var options = { url: "test.php", type: "post", dataType: "json", data: "country=" + $(this).val(), //build your data string here success: function (json) { $("#textbox").val(json.country); } }; $.ajax(options); } ); }); India phil japan
你可以这样做。 (jQuery Ajax示例)
关于下拉选择:
JS代码
$(document).ready(function() { $('city').onchange(function() { $.ajax({ type: "GET", url: "some.php", data: { city: this.val() } }).done(function( msg ) { alert( "Data Saved: " + msg ); }); }); });
PHP
some.php
您可以使用GET或REQUEST方法获取值。
$city = $_GET['city']; $city = $_REQUEST['city'];
我将采用相同的例子并修改并给你一个答案。
为了捕获php文件中服务器端的值
$var = $_POST['country']; $json = array('country' => $var); header("Content-Type: application/json"); echo json_encode($json);
更新后的代码将通过json
将country
返回给脚本,并写入 field,其中包含
id=textbox