[英]calling a php function by press the button
我用php編寫了以下代碼,在此代碼中,我想計算水費
但是當我按下按鈕時,結果不會顯示問題所在。 知道我要使用用戶輸入的值。
<!DOCTYPE html>
<html>
<body>
<form>
القيمة: <input type="text" name="cal"><br>
<button onclick="myFunction("cal")">إحسب التكلفة</button>
</form>
<script>
function myFunction($x) {
$x=$x%1000;
if($x<=15){
$x=$x*0.10;
return $x;}
elseif($x>=16&&$x<=30){
$x=$x*1.00;
return $x;}
elseif($x>=31&&$x<=45){
$x=$x*3.00;
return $x;}
elseif($x>=46&&$x<=60){
$x=$x*4.00;
return $x;}
else{
$x=$x*6.00;
return $x;}
}
</script>
</body>
</html>
實現此目的的另一種方法是將onClick事件添加到您的按鈕和調用函數中。
例。
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("myBtn").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
首先,javascript中沒有elseif
運算符。 如果使用,則應使用else if
。 另外,您還需要從輸入中獲取值並將其傳遞給myFunction
。 我修改了您的樣本,請檢查此插件
請嘗試以下腳本。 問題是您使用elseif代替if else if並且您也將該函數稱為錯誤。 我在輸入字段中添加了一個ID。 請檢查
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" name="cal" id="cal"><br>
<button type=button onclick="document.getElementById('data').innerHTML = myFunction(document.getElementById('cal').value)">إحسب التكلفة</button>
<hr>
Result:<span id="data"></span>
</form>
<script>
function myFunction($x) {
$x = $x % 1000;
if ($x <= 15) {
$x = $x * 0.10;
return $x;
}
else if ($x >= 16 && $x <= 30) {
$x = $x * 1.00;
return $x;
}
else if ($x >= 31 && $x <= 45) {
$x = $x * 3.00;
return $x;
}
else if ($x >= 46 && $x <= 60) {
$x = $x * 4.00;
return $x;
}
else {
$x = $x * 6.00;
return $x;
}
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.