使用Ajax将PHP值传递给javascript(Pass PHP value to javascript using Ajax)
我有一个提供数据库值的PHP代码。 我需要javascript变量中的那些值。
Javascript代码
<script src="http://code.jquery.com/jquery-1.8.0.js"></script> <script type="text/javascript"> function text() { var textVal=$("#busqueda_de_producto").val(); $.ajax( { type:"POST", url:"index.php", //here goes your php script file where you want to pass value data: textVal, success:function(response) { // JAVSCRIPT VARIABLE = varable from PHP file. } }); return false; } </script>
PHP文件代码:
<?php $q11 = "select * from sp_documentocompra_detalle where dcd_codigo".$_GET['codigo']; $res11 = mysql_query($q11); $row11 = mysql_fetch_array($res11); ?>
I have a php code that provides the database values. I need those values in the javascript variable.
Javascript Code
<script src="http://code.jquery.com/jquery-1.8.0.js"></script> <script type="text/javascript"> function text() { var textVal=$("#busqueda_de_producto").val(); $.ajax( { type:"POST", url:"index.php", //here goes your php script file where you want to pass value data: textVal, success:function(response) { // JAVSCRIPT VARIABLE = varable from PHP file. } }); return false; } </script>
PHP FILE CODE:
<?php $q11 = "select * from sp_documentocompra_detalle where dcd_codigo".$_GET['codigo']; $res11 = mysql_query($q11); $row11 = mysql_fetch_array($res11); ?>
原文:
最满意答案
由于
observe
选项的收集处理能力有限(所以你需要写"observe": [ "people[0].age","people[1].age", etc ]
使它像你一样工作已经弄明白了)推荐的模式是为你的people
收藏品使用create
功能。在您的自定义
create
创建函数中,您可以使用{ observe: ["age"] }
作为映射选项再次调用ko.mapping.fromJS
,该选项将创建所需的项:var data = { people: [ {id: 1, age: 25}, {id: 2, age: 35} ] }; var mappingOptions = { people: { create: function(options) { return ko.mapping.fromJS(options.data, { observe: ["age"] }); } } } var r = ko.mapping.fromJS(data, mappingOptions);
演示JSFiddle 。
Because of the limited collection handling capabilities of the
observe
option (so you would need to write"observe": [ "people[0].age","people[1].age", etc ]
to make it work as you've already figured it out) the recommended pattern is to use acreate
function for yourpeople
collection.In your custom
create
create function you can callko.mapping.fromJS
again with your{ observe: ["age"] }
as the mapping option which will create the required items:var data = { people: [ {id: 1, age: 25}, {id: 2, age: 35} ] }; var mappingOptions = { people: { create: function(options) { return ko.mapping.fromJS(options.data, { observe: ["age"] }); } } } var r = ko.mapping.fromJS(data, mappingOptions);
Demo JSFiddle.
相关问答
更多-
使用ko.mapping使knockout observablearray项目可观察(Make knockout observablearray items observable with ko.mapping)[2024-05-09]
为什么你需要ko.mapping ,你不想只想将initialData映射为child实例吗? self.children = ko.observableArray(initialData.map(function(e){ return new child(e); })); 请参见Array.map Why do you need ko.mapping, ... -
好吧,按照@Salvador Dali的建议,我正在创建一个更可重复的例子,通过这样做我可以找到解决方案。 由于我使用require.js加载我的库,我发现敲除有问题。 虽然在我的index.jsp中将knockout作为脚本包含在内,但它无法正常工作。 解决方案包括在main.js(boilertplatejs文件)中配置knockout,如下所示: paths:{ … knockout : 'path_to_knockout js', knockout_m ...
-
你也应该为孩子们提供映射选项。 尝试更新computed函数如下: var computed = function (data) { var self = this; ko.mapping.fromJS(data, mapping, self); self.Uri = ko.computed(function () { ...
-
我不认为ignore选项与点符号兼容。 尝试这个: ko.mapping.toJS(viewModel.result(), { 'ignore': ["Entity"] }, this) I don't think the ignore option is compatible with dot notation. Try this: ko.mapping.toJS(viewModel.result(), { 'ignore': ["Entity"] }, this)
-
您需要创建新的客户视图模型。 你可以使用这样的现有映射来完成它 var customer = mapping.customers.create({ data: { CustomerId: "", FirstName: "", LastName: "", StoreCustomerId: "" }}); 然后将其推送给现有客户并选择: self.customers.push(customer); self.selectedCustomer(customer); 看到更新的小 ...
-
你可以在调用ko.mapping.fromJS后添加扩展器 var applyMapping = function(rawData) { ko.mapping.fromJS(rawData, {}, this); this.name = this.name.extend({e2ElementName: true}); } 或者您可以使用映射插件选项 var mappingOptions = { // customize the creation of the name property n ...
-
在某些项目中,有时您从服务器返回的数据对象很复杂,具有许多属性,而这些属性又具有更多属性。 您可能希望可以观察到所有或大量的可观察对象,以便UI对变化做出反应。 手动创建所有这些可观察对象并分配值是一项繁琐的工作。 映射插件允许您自动为数据对象的属性生成可观察对象。 您可以使用插件执行各种自定义操作,例如忽略某些属性,或选择哪些属性不会变得可观察而不仅仅是普通属性。 这里的要点是底层行为完全相同。 它是所有可观察的,只是以不同的方式创建。 有关示例,请参阅Knockout文档的映射 。 就个人而言,我以前没 ...
-
也许有更好的解决方案。 您可以通过这种方式调用映射: ko.mapping.fromJS(items, mapping,vm.items); mapping是一个具有create函数的对象: var mapping = { create: function(options) { return new dummyModel(options.data); } } 而dummyModel是一个临时对象,而不是包含你想要添加的新属性,以及来自插件的属性: var dum ...
-
你的小提琴里有很多错误/错误。 首先,这是一个工作的 。 这是你的错误。 jsfiddle没有正确设置。 要正确设置它,请在Javascript设置中选择库Knockout 3.4 。 之后,您不需要直接引用js文件。 此外,您根本没有引用映射插件。 最后,你不需要jQuery文档就绪包装,特别是当jQuery甚至没有被引用时。 映射插件从对象映射到对象,默认情况下必须具有匹配的属性名称,这些属性区分大小写。 因此,您必须从GetData提供适当的源。 GetData方法没有返回类似promise的对象,因 ...
-
由于observe选项的收集处理能力有限(所以你需要写"observe": [ "people[0].age","people[1].age", etc ]使它像你一样工作已经弄明白了)推荐的模式是为你的people收藏品使用create功能。 在您的自定义create创建函数中,您可以使用{ observe: ["age"] }作为映射选项再次调用ko.mapping.fromJS ,该选项将创建所需的项: var data = { people: [ {id: 1, age: 2 ...