下拉框中更改输入框的选项

发布于 2024-10-31 16:28:54 字数 1635 浏览 3 评论 0原文

天啊,

我正在为我的页面开发一个在线捐赠网站。我希望用户有一个带有付款选项的下拉框(5 美元、10 美元、20 美元、其他)。我目前已将其设置为在选择“其他”选项时显示输入框的位置;如果选择另一个选项($5、$10、$20),则隐藏输入框。此外,捐款是通过 PayPal 进行的,我已将 PayPal 捐款按钮/php 合并到网站中。我遇到了以下问题:因为输入框是填充捐赠金额的字段,所以 PayPal 无法识别下拉金额。例如,如果我选择“其他”选项,请在输入框中输入 100 美元,然后从下拉框中选择“10 美元”选项(假设我改变了主意,想捐赠 10 美元而不是 100 美元),然后单击捐赠按钮,PayPal 处理 100 美元而不是 10 美元的付款(因为它是从输入框中提取的)。

我相信我有一个解决方案,但我不一定确定如何编码。我正在寻求以下方面的帮助:当用户从下拉框中选择一个选项时,我希望此操作使用相应的值填充(隐藏)输入框。同样,如果用户从下拉列表中选择“其他”选项,我希望输入框(现在可见)填充“”,因为用户将自己填充此选项。

再说一次,我不确定如何编写这样的代码,我希望有人能指出我正确的方向。我真诚地感谢您的帮助,并且非常珍惜您的时间。谢谢你!

编辑:谢谢您的帮助。这是我现在拥有的代码:

    <select name="amount" id="otherFieldOption" class="dropdown">
                    <option value="20">$20</option>
            <option value="10">$10</option>
            <option value="5" selected="selected">$5</option>
            <option value="otherField">Other</option>   
    </select>

    <div id="otherField">
        Amount:
        <input type="text" id="amount" name="amount" class="textfield" size="10"/>
    </div>

jquery 代码:

$(document).ready(function() {
  $.viewInput = {
    '0' : $([]),
    //THIS IS THE NAME OF THE DIV WRAPPING THE HIDDEN FIELD
    'otherField' : $('#otherField'),
  };

$('#otherFieldOption').change(function() {
    // HIDES THE INPUT FIELD IF ANOTHER DROPDOWN ITEM IS SELECTED ONCE THE HIDDEN FIELD IS LOADED
    $.each($.viewInput, function() { this.hide(); });
    // SHOWS THE INPUT FIELD ITEM IF SELECTED
    $.viewInput[$(this).val()].show();
  });

});

Hellp,

I am working on developing an online donation site for my page. I'd like the user to have a dropdown box with payment options ($5, $10, $20, Other). I've currently set it up to where it displays an input box if the "other" option is selected; if another option is selected ($5, $10, $20) then the input box is hidden. Furthermore, donations are taken via pay-pal and I've incorporated the Pay-Pal donate button/php into the site. I've run across the following issue: because the input box is the field that populates the amount that is being donated Pay-Pal does not recognize the dropdown amounts. For example, if I choose the "Other" option, enter $100 into the input box, then select the "$10" option from the dropdown box (let's assume I changed my mind and wanted to donate $10 instead of $100), and click the donate button, Pay-Pal processes the payment for $100 not $10 (because it's pulling from the input box).

I believe I have a fix, but I'm not necessarily sure how to code it. I am looking for help with the following: When a user selects an option from the dropdown box I would like this action to populate the (hidden) input box with the respective value. Similarly, if the user selects the "other" option from the dropdown, I'd like the input box (now visible) to be populated with "" as the user would populate this option him/her-self.

Again, I'm not certain how to code something like this, and I'm hopeful someone could point me in the right direction. I really sincerely appreciate any help, and I'm very gareful for your time. Thank you!

edit: Thank you for your help. Here is the code that I have as of right now:

    <select name="amount" id="otherFieldOption" class="dropdown">
                    <option value="20">$20</option>
            <option value="10">$10</option>
            <option value="5" selected="selected">$5</option>
            <option value="otherField">Other</option>   
    </select>

    <div id="otherField">
        Amount:
        <input type="text" id="amount" name="amount" class="textfield" size="10"/>
    </div>

the jquery code:

$(document).ready(function() {
  $.viewInput = {
    '0' : $([]),
    //THIS IS THE NAME OF THE DIV WRAPPING THE HIDDEN FIELD
    'otherField' : $('#otherField'),
  };

$('#otherFieldOption').change(function() {
    // HIDES THE INPUT FIELD IF ANOTHER DROPDOWN ITEM IS SELECTED ONCE THE HIDDEN FIELD IS LOADED
    $.each($.viewInput, function() { this.hide(); });
    // SHOWS THE INPUT FIELD ITEM IF SELECTED
    $.viewInput[$(this).val()].show();
  });

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫雨熙 2024-11-07 16:28:54

这是一个 演示

<select id="choice">
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
        <option value="other">other</option>
</select>
<input type='text' id="other" class="hidden" />

和 jquery

$('#choice').change(function(){
    var selected_item = $(this).val()

    if(selected_item == "other"){
        $('#other').val("").removeClass('hidden');
    }else{
        $('#other').val(selected_item).addClass('hidden');
    }
});

here is a demo

<select id="choice">
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
        <option value="other">other</option>
</select>
<input type='text' id="other" class="hidden" />

and jquery to go with

$('#choice').change(function(){
    var selected_item = $(this).val()

    if(selected_item == "other"){
        $('#other').val("").removeClass('hidden');
    }else{
        $('#other').val(selected_item).addClass('hidden');
    }
});
老街孤人 2024-11-07 16:28:54

当用户单击选择框时清除文本框。

尝试以下操作:

<select id="moneySelect" onchange="$('#moneyText').val(''); $('#sendValue').val(this.value);">
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
</select>
<input type='text' id="moneyText" onkeypress="$('#sendValue').val(this.value)">
<input type="hidden" name="sendValue" id="sendValue" />

When the user clicks the select box clear the textbox.

Try this:

<select id="moneySelect" onchange="$('#moneyText').val(''); $('#sendValue').val(this.value);">
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
</select>
<input type='text' id="moneyText" onkeypress="$('#sendValue').val(this.value)">
<input type="hidden" name="sendValue" id="sendValue" />
人生戏 2024-11-07 16:28:54
<label>Donetion:</label>
    <input list="browsers" name="myBrowser" placeholder="Donetion" />
    <datalist id="browsers">
    <option value="$5">
    <option value="$15">
</datalist>
<label>Donetion:</label>
    <input list="browsers" name="myBrowser" placeholder="Donetion" />
    <datalist id="browsers">
    <option value="$5">
    <option value="$15">
</datalist>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文