[英]javascript keypress event and php
HTML代碼:
<td><input type="text" name="flat[]" class="" value="<?php echo $row_pdetails["flat"]; ?>" /></td>
<td><input type="text" name="new_price[]" class="" value="<?php echo $row_pdetails["price"]; ?>" /></td>
<td><input type="text" name="org_price[]" class="" value="<?php echo $row_pdetails["org_price"]; ?>" readonly/></td>
<td><input type="submit" name="save" value="save"/></td>
PHP代碼:
<?php
if(isset($_POST["save"]))
{
for($i=0;$i<count($_POST["org_price"]); $i++)
{
$org_price=$_POST["org_price"][$i];
$new_price=$_POST["new_price"][$i];
mysql_query("update tb_party_rate_entry set price='$new_price' where (party_id='$p_id' && org_price='$org_price')");
}
}
在此代碼中,我使用while循環從數據庫中獲取new_price[]
和org_price[]
的值,並且為了更新數據庫中的這些值,我在PHP代碼中使用了for循環。
現在我想要的是,每當我在flat[]
文本框中輸入任何值時,都應該使用new_price[]
事件同時在new_price[]
文本框中輸入相同的值。 當我在第一行的固定價格中輸入值時,應在第一行的new_price[]
輸入值;當我在第二行的固定價格中輸入值時,應將其輸入第二行的new_price[]
,並在按鍵事件中輸入。
請幫忙。
在這里測試: http : //jsfiddle.net/qsDn5/3/
HTML
<div>
<tr>
<td>flat1:<input type="text" name="flat[]" class="flat" value=""/></td>
<td>price1:<input type="text" name="new_price[]" class="new_price" value=""/></td>
<td><input type="text" name="org_price[]" class="" value=""</td>
</tr>
</div>
<div>
<tr>
<td>flat2:<input type="text" name="flat[]" class="flat" value=""/></td>
<td>price2:<input type="text" name="new_price[]" class="new_price" value=""/></td>
<td><input type="text" name="org_price[]" class="" value=""</td>
</tr>
</div>
<p><input type="submit" name="save" value="save"/></p>
jQuery的
$(document).ready(function(){
$('.flat').on('keyup',function(e) {
$( this ).siblings(".new_price").eq(0).val ( $(this).val() );
});
});
您應該在flat []上使用onchange事件,並因此設置new_price []的值。
就像是
$('input[name="flat[]"]').on('keyup',function(e) {
$( this ).siblings('input[name="new_price[]"]:first').val ( $(this).val() );
});
注意:我使用了兄弟姐妹,因為我假設您將使用相同的輸入名稱來擁有不同的行,這樣,您只更新了正確的元素
嘗試這個:
請注意,我在html中添加了rel
屬性並為textbox創建了唯一的ID。意味着每個文本框都有一個唯一的ID。
<?php
$i = "1";
while(//$row=...){
?>
<td><input type="text" name="flat[]" id="flat<?php echo $i;?>" rel="<?php echo $i;?>" class="flat" value="<?php echo $row_pdetails["flat"]; ?>" /></td>
<td><input type="text" name="new_price[]" id="newprice<?php echo $i;?>" rel="<?php echo $i;?>" class="new_price" value="<?php echo $row_pdetails["price"]; ?>" /></td>
<td><input type="text" name="org_price[]" id="orgprice<?php echo $i;?>" rel="<?php echo $i;?>" class="org_price" value="<?php echo $row_pdetails["org_price"]; ?>" readonly/></td>
<?php $i++;} ?>
<td><input type="submit" name="save" value="save"/></td>
JQuery:
當用戶嘗試在平面文本框中輸入某些內容時,我們得到了它的rel
和價值。使用相關性的值,我們發現了相關的newprice文本框。
$(document).on('keyup','.flat',function(){
var rel = $(this).attr('rel');
var flatvalue = $(this).val();
$("#newprice"+rel).val(flatvalue);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.