2016-07-23 2 views
0

私は最初にボタンのテキスト値を取得し、それから変数の値を決定する私のWordPressサイト用の関数を記述しようとしています。変数の値を取得するためにボタンのテキスト値を取得する

私には3つのボタンがあります。 それぞれに同じテキスト値「項目の選択」があります。私はクリックしたときにテキスト値を "Selected"に変更するjavascript関数を作成しました。今度は、ボタンのテキスト値が "Selected"に変更されたかどうかを判断し、$ amountという変数を新しい値に変更する方法を見つける必要があります。私はこれがjqueryで行うことができることを知っているが、私がそれを回避しようとしている理由は、特定のwordpress関数で動作する必要があり、wordpressのfunctions.php内のスクリプトを呼び出すのが混乱するからです。私はボタンを入力に変更してフォームに入れようとしましたが、$ _POSTメソッドを試すことができましたが、$ _POSTメソッドはサブミットのためだけに機能し、サブミットする前に変数を変更する必要があります。

<a href="#" class="selectitem1" type="submit">Select Item</a> 
<a href="#" class="selectitem2" type="submit">Select Item</a> 
<a href="#" class="selectitem3" type="submit">Select Item</a> 

$(function() {      
    $("a.selectitem1").click(function() { 
    $("a.selectitem1").text('Selected'); 
    }); 
}); 

//not sure what to do here 
add_filter('abcde', 'adjust_amount'); 
function adjust_amount() { 

    if selectitem1 text value is now "Selected"{ 
    $amount = 50; 
    if selectitem2 text value is now "Selected"{ 
    $amount = 150; 
    if selectitem3 text value is now "Selected"{ 
    $amount = 250; 
} 
+0

jsからphpを呼び出す場合は、AJAXが必要です。あなたがここで試していることは、実際には意味をなさない。 – Ven

答えて

1

あなたは既にページにjQueryを含めているように見えるので、あなたは$amount変数を設定するには、jQueryのを使用しないだろう、なぜ私は表示されないのですか?次のように

所望の機能性を得るための最も簡単な方法、次のようになります。

$(function() {      
    $('a.selectitem1').click(function() { 
     $('a.selectitem1').text('Selected'); 
     $('a.selectitem2').text('Select item'); 
     $('a.selectitem3').text('Select item'); 
     $amount = 50; 
    }); 

    // equivalently for selectitem2 and selectitem3 
}); 
+0

これは変数$ amountがその関数のローカルなので、あなたが知っている限り、あなたのwordpressサイトのfunctions.phpファイルにjavascript関数を追加することはできません。残念ながら、上記は私のためにはうまくいかないでしょう。 – LearntoExcel

+0

おそらく、隠された ''の値を$ amountに設定し、その値を他の関数から読み取ることができますか? – Bart

+0

こんにちはバート、それはとても良いアイデアです。私はそれを次に試してみましょう。ありがとうございました。 – LearntoExcel

0

私はAJAXを使用します。

フォームファイルでは、次の:

<!DOCTYPE html> 
<html> 
<body> 

<form action=""> 
<input type="button" id="txt1" value="Select Item" name="£10.00" onclick="showValue(this.name)"> 
<input type="button" id="txt1" value="Select Item" name="£15.00" onclick="showValue(this.name)"> 
<input type="button" id="txt1" value="Select Item" name="£20.00" onclick="showValue(this.name)"> 
</form> 

<p>Value: <span id="txtValue"></span></p> 

<script> 
function showValue(str) { 
    var xhttp; 
    if (str.length == 0) { 
    document.getElementById("txtValue").innerHTML = ""; 
    return; 
    } 
    xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     document.getElementById("txtValue").innerHTML = xhttp.responseText; 
    } 
    }; 
    xhttp.open("GET", "getinfo.php?q="+str, true); 
    xhttp.send(); 
} 
</script> 

</body> 
</html> 

次に、getinfo.phpというファイルを作成し、次のファイルを追加します。

<?php 
// Array with names 
$a[] = "£10.00"; 
$a[] = "£15.00"; 
$a[] = "£20.00"; 


// get the q parameter from URL 
$q = $_REQUEST["q"]; 

$value = ""; 

if ($q !== "") { 
    $q = strtolower($q); 
    $len=strlen($q); 
    foreach($a as $name) { 
     if (stristr($q, substr($name, 0, $len))) { 
      if ($value === "") { 
       $value = $name; 
      } else { 
       $value .= ", $name"; 
      } 
     } 
    } 
} 

echo $value === "" , $value; 
//More code here for your selected $value variable 
?> 
関連する問題