search
HomeBackend DevelopmentPHP Tutorial关于ajax问题,在线等,先谢谢各位了

index.php页面有两个div,ID分别为div1,div2,对应的事件为:

$.ajax({		url: test.php,		type: 'post',		dataType:'text',		success: function (responseText) {			$('#div1').html(responseText);			$('#div2').html(responseText);		}	});

有没有办法将test.php页面返回的值分别放到div1,div2呢?我想到的是将返回的值用js来切隔,然后放到里面,还没有实践,不知行不行,如果可以,我觉得这样好像不是很好。请问有其他的方法吗?


回复讨论(解决方案)

完全可以,只是你要注意

responseText

里面,不能包含你需要切割的那个字符。


推荐你用json,这样比较方便些。就不会涉及切割了。

可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了

返回多个数据一般用 json

$res = array(  'div1' => '相关内容',  'div2' => '相关内容',)echo json_encode($res);

$.ajax({  url: 'test.php',  type: 'post',  dataType:'json',  success: function (data) {     $('#div1').html(data.div1);     $('#div2').html(data.div2);  }});

更一般的
$.post('test.php', {}, function(d) {  for(var i in d) $('#'+i).html(d[i]);}, 'json');

完全可以,只是你要注意

responseText

里面,不能包含你需要切割的那个字符。


推荐你用json,这样比较方便些。就不会涉及切割了。


为什么还“不能包含你需要切割的那个字符。”?比如说我得到的“中华人民共和国{|}国和共民人华中”,要有{|}这个才好切啊。

完全可以,只是你要注意

responseText

里面,不能包含你需要切割的那个字符。


推荐你用json,这样比较方便些。就不会涉及切割了。


好的,我先试试哈

完全可以,只是你要注意

responseText

里面,不能包含你需要切割的那个字符。


推荐你用json,这样比较方便些。就不会涉及切割了。


刚刚回的应该是回复你,哈哈,成功完成,谢谢。


完全可以,只是你要注意

responseText

里面,不能包含你需要切割的那个字符。


推荐你用json,这样比较方便些。就不会涉及切割了。


为什么还“不能包含你需要切割的那个字符。”?比如说我得到的“中华人民共和国{|}国和共民人华中”,要有{|}这个才好切啊。

假设你要按"|"切割,这样你看对不对:

//显然是对的
这是测试数据|这是测试数据

//这样就不对了
这是|测试|数据|这是|测试|数据

所以你还是用json吧,方便

用json,根?key?取value,??就方便了。

基本同意楼上观点,用json 键值对可以处理很多问题比text好用

可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了

返回多个数据一般用 json

$res = array(  'div1' => '相关内容',  'div2' => '相关内容',)echo json_encode($res);

$.ajax({  url: 'test.php',  type: 'post',  dataType:'json',  success: function (data) {     $('#div1').html(data.div1);     $('#div2').html(data.div2);  }});

更一般的
$.post('test.php', {}, function(d) {  for(var i in d) $('#'+i).html(d[i]);}, 'json');


奇怪了,我回复了两次,是引用你的,都变成第一个回复的了。


可以用 js 切割,但你得有唯一的切割标志吧?
不然把正文也切割了,就不美了

返回多个数据一般用 json

$res = array(  'div1' => '相关内容',  'div2' => '相关内容',)echo json_encode($res);

$.ajax({  url: 'test.php',  type: 'post',  dataType:'json',  success: function (data) {     $('#div1').html(data.div1);     $('#div2').html(data.div2);  }});

更一般的
$.post('test.php', {}, function(d) {  for(var i in d) $('#'+i).html(d[i]);}, 'json');


奇怪了,我回复了两次,是引用你的,都变成第一个回复的了。完美解决,谢谢。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool