2012-08-26 14 views
0

所以基本上這就是我想做的事:如何將jQuery和PHP結合起來,根據用戶選擇動態更改輸入值?

我有一個名爲$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'. 
} 

樣本輸入選擇我想要的目標是:

<select id="sub_cat" style="" name="sub_cat"> 
<option selected="selected" value="-10">All</option> 

如果$dynamicchangeen,它保持,因爲它是如果是tr,則更改爲Hepsi

我該如何去做這件事?任何幫助表示讚賞。

回答

0

我不明白你想要什麼,但就我的理解,你可以做這樣的事情

把價值在一個隱藏的輸入

<input id="test" hidden="hidden" value="$dynamicchange"> 

然後做這樣的事情

$(function(){ 
    var herp = $('#test').val(); 
    if(herp=="en"){ 
     //do stuff 
    }else{ 
     //do other stuff 
    } 
}); 

你可以有一個計數器(不是1毫秒你的電腦會爆炸),它再次觸發功能。你不清楚這個功能是如何開始的,所以我把它留給你的判斷(我會做window.load())

+0

沒有什麼幫助,但仍然是一個很好的答案。 –

0

連接到通常不能健康的服務器。

你可以在這種情況下使用Ajax的jQuery的,如下圖所示:

$.get('phpfile', { send your data }, function(returned) { if returned == en ... }); 
0

如果我理解正確的話,我認爲你應該使用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"; 
} 

在你必須有標識元素的JavaScript就能夠認識到,像HTML文檔:

<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--> 

Finaly您show.php文件應該看起來像這樣:

<?php 
$contentVar=$_POST['contentVar']; 
if ($contentVar=='id1') { 
echo 'Text that you want to be displayed instead';} 
//you can use multiple 'else if' loops afterwards 
?> 

希望有所幫助。

相關問題