簡體   English   中英

將輸入值作為參數傳遞給按鈕中的JavaScript函數

[英]Pass input value to a JavaScript function in a button as a parameter

我有一個可以在其中插入數字的字段,我想將此數字作為參數傳遞給調用函數的按鈕。 我嘗試了getElementById但沒有按預期工作。

我的代碼在php內部被調用。 我從數據庫中獲得前兩個參數(名稱,價格),並且我希望從輸入中獲得第三個參數(數量)。 可能嗎?

<input type="number"  min="0" style ="width:4em" id="quantity" name="quantity" />
<input type="button" value="Add to Cart" onclick="AddtoCart(\''.$name.'\',\''.$price.'\',quantity); " />

來自php的變量必須使用php的開始和結束標簽正確地傳遞到html:

<input type="number"  min="0" style ="width:4em" id="quantity" name="quantity" />
<input type="button" value="Add to Cart" onclick="AddtoCart('<?php echo $name; ?>', '<?php echo $price; ?>',document.getElementById('quantity').value); " />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM