[英]Grab and send a text value via a link using Javascript
我正在嘗試通過鏈接將輸入的文本發送到框中。 請在下面查看我的代碼。 由於某種原因它沒有通過發送嗎?
<script>
var discountCode = document.getElementById("discountCode").value;
openPage = function() {
location.href = "payment.php?discount="+discountCode;
}
</script>
<a href ="javascript:openPage()" class="btn btn-warning">Purchase</a>
您將在頁面加載后立即讀取該值,而不是單擊鏈接時。 您只需要將行移動到函數中:
<script>
openPage = function() {
var discountCode = document.getElementById("discountCode").value;
location.href = "payment.php?discount="+discountCode;
}
</script>
<a href ="javascript:openPage()" class="btn btn-warning">Purchase</a>
或可替代先手元素在頁面加載時(假設這個腳本是在經過了參考discountCode
元素),然后閱讀函數的值:
<script>
var discountCode = document.getElementById("discountCode");
openPage = function() {
location.href = "payment.php?discount="+discountCode.value;
}
</script>
<a href ="javascript:openPage()" class="btn btn-warning">Purchase</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.