我想在我的非營利網站上有一個網站捐贈表單,我希望人們能夠通過信用卡選擇一次性禮物或月度禮物。如果他們選擇月度禮物,我需要將一些值發送到付款處理器,並且一個很好的方法可能會爲隱藏的表單字段切換禁用的屬性。我爲此輸入了兩個setAttribute()javascript函數,還有兩個用於單擊單選按鈕的事件偵聽器。問題是添加這兩個函數和兩個監聽器似乎已經這樣做了,因此我只能點擊一次「每月(重複)禮物」選項。 '單一禮物'開始檢查,如果我然後點擊'每月(定期)禮物',然後點擊'單一禮物',瀏覽器將不會讓我再次選擇'每月(定期)禮物'選項。如果我刪除的JavaScript,它會讓我這樣做。爲什麼?爲什麼我的javascript阻止我多次選擇一個單選按鈕?
這裏是我的代碼使用javascript:
<form>
<input type="hidden" name="OverRideRecureDay"
id="overriderecureday" value="Y" disabled="disabled">
<input type="hidden" name="RID" id="rid" value="your_RID#"
disabled="disabled">
<input type="hidden" name="recur_times" id="recur_times"
value="your_recur_times" disabled="disabled">
<div id="giftfrequency">
<div id="giftfrequencylabel" class="editor-label2">Gift Frequency:
</div>
<div class="editor-radio">
<input type="radio" id="onetimedonation" name="override_recur"
value="" checked="checked"></input> <span>Single Gift</span>
</div>
<div class="editor-radio">
<input type="radio" id="recurringdonation" name="override_recur"
value="Y"></input> <span>Monthly (Recurring) Gift</span>
</div>
</div>
</form>
<script>
var MonthlyGift = document.getElementById("recurringdonation");
var OneTimeGift = document.getElementById("onetimedonation");
var OvrRecurDay = document.getElementById("overriderecureday");
var RecurTimes = document.getElementById("recur_times");
var RID = document.getElementById("rid");
function EnableRecurring() {
MonthlyGift.setAttribute("disabled", "");
OvrRecurDay.setAttribute("disabled", "");
RecurTimes.setAttribute("disabled","");
};
function DisableRecurring() {
RID.setAttribute("disabled", "disabled");
OvrRecurDay.setAttribute("disabled", "disabled");
RecurTimes.setAttribute("disabled","disabled");
};
document.getElementById("recurringdonation").addEventListener("click", EnableRecurring);
document.getElementById("onetimedonation").addEventListener("click", DisableRecurring);
</script>
的jsfiddle與JavaScript的情況是http://jsfiddle.net/s1011205/73zstxz0/1/
的jsfiddle對於沒有JavaScript的場景http://jsfiddle.net/s1011205/5b47abz0/3/
我在其他地方找了一個很好的答案,但我不確定我是否成功。一個人在this one中使用Bootstrap。 This one不適用,因爲我的兩個單選按鈕確實具有相同的名稱。
哇。你是對的。我在EnableRecurring函數中有「MonthlyGift.setAttribute(」disabled「,」「)」,我應該有「RID.setAttribute(」disabled「,」「)」。那是我的問題,現在問題解決了。感謝您收到我的(自己產生的)問題。 –
當然可以!只要確保那些你使用'RID.removeAttribute(「disabled」);'如果您嘗試啓用(取消禁用)這些元素。 – Sam
Sam,你的意思是說,關於什麼傳遞給服務器,當一個命中表單提交buttom時,setAttribute(「disabled」,「」)不等於removeAttribute(「disabled」)?我認爲將disabled屬性值設置爲空(「」)就足夠了。 (當我在11月份開始認真爲我的非營利組織建立網站時,我才真正開始真正學習編碼(在那之前沒有重要的編碼培訓或教育))。 –