[英]how to pass the parameter in function
如何傳遞文本框的值,這對於第一個選擇框有效,為什么不對所有框都有效。如果我選中復選框,則文本框值將設置為null
<input type="checkbox" name="onoffswitch'.$i.'" class="onoffswitch'.$i.'-checkbox" id="myonoffswitch'.$i.'" checked onclick=my(this.form,'buy".$p."','sell".$p."','onoffswitch".$i."');>
echo '<td><input type=text maxlength="6" size="6" value='.$cfet['purchaseprice'].' name=buy'.$p.'></td>';
echo '<td><input type=text maxlength="6" size="6" value='.$cfet['sellingprice'].' name=sell'.$p.'></td>';
function my(a,buy,sell,select)
{
alert("select"+select);
alert("si"+a.select.checked);
if(a.select..checked == false) {
alert("true"+buy);
a.buy.value="";
a.sell.value="";
}
在Javascript(和JQuery)中,您可以簡單地通過在函數名稱中包含變量並像普通ie一樣進行引用來傳遞值。
my(this.val,$,$,"x")
function my(value,something,somethingElse,letter){
var answer = somthing + somethingElse;
alert(value);
}
PHP代碼不正確,請使用<?php /*code*/ ?>
HTML和PHP代碼
<input type="checkbox" name="onoffswitch<?php echo $i ?>"
class="onoffswitch<?php echo $i ?>-checkbox"
id="myonoffswitch<?php echo $i; ?>" checked
onclick="my(this.value,'buy<?php echo $p ?>','sell<?php echo $p ?>');">
PHP
回聲
<?php
echo "<input type=\"checkbox\" name=\"onoffswitch$i\"
class=\"onoffswitch$i-checkbox\"
id=\"myonoffswitch$i\" checked
onclick=\"my(this.value,'buy$p','sell$p')\">";
您詢問如何將文本框的值作為參數傳遞
您必須像這樣通過它:
onclick="my(this.value,"'.$cfet['purchaseprice'].'","'.$cfet['sellingprice']");
這是使用jQuery獲取input
的值的方法。 假設您的復選框是當前上下文(在上面的例子中是):
onclick="my(this.value, $(this).next().val(), $(this).nextAll().eq(1).val())"
如果您使用jQuery附加事件處理函數,則可以提高可讀性和易於維護:
$('#myonoffswitch').on('click', function(){
var a = this.value, b = $(this).next().val(), c = $(this).nextAll().eq(1).val();
alert(a + ' ' + b + ' ' + c);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.