如何结合jQuery和PHP根据用户选择动态更改输入值?
所以基本上这就是我想做的: 我有一个名为如何结合jQuery和PHP根据用户选择动态更改输入值?,php,javascript,jquery,html,Php,Javascript,Jquery,Html,所以基本上这就是我想做的: 我有一个名为$dynamicchange的php变量,它根据不同的用户选择(他们在哪个页面等)获取不同的值 我还有一些选择选项表单输入,其中包含“全部”作为条目 使用jQuery,我想做到以下几点: [every 1 millisecond, check for and update all input fields if needed] << function if ($dynamicchange == 'en') { do not replace
$dynamicchange
的php变量,它根据不同的用户选择(他们在哪个页面等)获取不同的值
我还有一些选择选项表单输入,其中包含“全部”作为条目
使用jQuery,我想做到以下几点:
[every 1 millisecond, check for and update all input fields if needed] << function
if ($dynamicchange == 'en') {
do not replace anything, the phrase stays as it is, 'All'.
}
if ($dynamicchange == 'tr') {
replace all input phrases every 1 millisecond, changing them to 'Hepsi'.
}
[每1毫秒,检查并更新所有输入字段(如果需要)]我不知道您到底想要什么,但据我所知,您可以这样做
$(function(){
var herp = $('#test').val();
if(herp=="en"){
//do stuff
}else{
//do other stuff
}
});
将值放入隐藏的输入中
<input id="test" hidden="hidden" value="$dynamicchange">
你可以有一个计数器(不是1毫秒你的电脑会爆炸),它会再次触发功能。您首先没有明确说明此函数将如何启动,因此我将此问题留给您判断(我将执行window.load())连接到通常无法正常运行的服务器
在这种情况下,您可以使用ajax jquery,如下所示:
$.get('phpfile', { send your data }, function(returned) { if returned == en ... } );
如果我理解正确,我认为您应该使用AJAX脚本,并为访问者的“选择”引入事件。您需要有单独的PHP页面,其中包含要显示的文本和元素。
您的Javascript应该如下所示:
function showText(cv){//cv is an element identifier which is posted to php page
var url='show.php';
$.post(url,{contentVar:cv}, function (data){
$('#textt').html(data).show();
});
}
function eraseText (){
document.getElementById('textt').innerHTML="What you want it to say by default";
}
<?php
$contentVar=$_POST['contentVar'];
if ($contentVar=='id1') {
echo 'Text that you want to be displayed instead';}
//you can use multiple 'else if' loops afterwards
?>
在HTML文档中,必须有Javascript能够识别的ID元素,例如:
<p onmouseover="JavaScript:showText('id1');" onmouseout="JavaScript:eraseText();">Some
text</p><!--this is the element upon which the actions taken will change the page, you
can have many such elements but you need to change the id inside ('id1')-->
<div id="textt"style="position: fixed; bottom: 20%; left: 20%; background: lightgrey;
width: 70%; height: 70%;">What you want it to say by default
<br /></div><!--this is the element that will be changed upon action, you can define
more such elements by adding more rows in the function-->
一些
正文
默认情况下,您希望它说什么
最后,show.php文档应该如下所示:
function showText(cv){//cv is an element identifier which is posted to php page
var url='show.php';
$.post(url,{contentVar:cv}, function (data){
$('#textt').html(data).show();
});
}
function eraseText (){
document.getElementById('textt').innerHTML="What you want it to say by default";
}
<?php
$contentVar=$_POST['contentVar'];
if ($contentVar=='id1') {
echo 'Text that you want to be displayed instead';}
//you can use multiple 'else if' loops afterwards
?>
希望有帮助