简体   繁体   English

检查选择了哪个单选选项并根据该选项显示一个按钮

[英]Check which radio option is selected and display a button based on that

I have a form, which contains a free option and a paid option.我有一个表格,其中包含一个免费选项和一个付费选项。

If the user selects a paid option, I want to display a different button than if they press the free option.如果用户选择付费选项,我想显示一个与他们按下免费选项不同的按钮。

form:形式:

<form method=POST>
    <input type="radio" name="plan" value="1days" required="required"> 1 day (free) &nbsp;
    <input type="radio" name="plan" value="7days" required="required"> 7 days  (£12.99)</p>
    <button type="submit" class="link button" display:block">Submit</button>
 </form>

What I want is something like:我想要的是这样的:

  <form method=POST>
    <input type="radio" name="plan" value="1days" required="required"> 1 day (free) &nbsp;
    <input type="radio" name="plan" value="7days" required="required"> 7 days  ($12.99)</p>
     // if radio value == "7days":
     <button type="submit" display:block">Pay 12.99 now </button>
     // else:
     <button type="submit" display:block">Submit</button>
 </form>

I am not sure how to achieve this in real time while the user is completing the form, I'm assuming its Javascript, but im not sure how to implement?我不确定如何在用户填写表格时实时实现这一点,我假设它的 Javascript,但我不确定如何实现? Thanks.谢谢。

EDIT:编辑:

The 12.99 button will take the person to a payment handler, whereas the other one should just complete/submit the form. 12.99按钮会将人带到付款处理程序,而另一个人应该只是填写/提交表格。

EDIT2:编辑2:

This is the payment processor that I use:这是我使用的支付处理器:

<form action="{% url 'charge' %}" method="post">
  {% csrf_token %}
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="{{ key }}"
          data-description="Website Purchase"
          data-currency=usd
          data-amount="{{ price }}"
          data-locale="auto"></script>

    <button type="submit" class="link button" style="margin:auto;">COMPLETE THIS ORDER</button>
</form>

So really I will have to replace the pay 12.99 button with the above form所以真的我必须用上面的表格替换pay 12.99按钮

you can do that directly in the onclick function:您可以直接在 onclick function 中执行此操作:

  <form method=POST>
    <input type="radio" name="plan" value="1days" required="required" onclick="document.getElementById('s2').style.display ='none';document.getElementById('s1').style.display =''"> 1 day (free) &nbsp;
    <input type="radio" name="plan" value="7days" required="required" onclick="document.getElementById('s1').style.display ='none';document.getElementById('s2').style.display =''"> 7 days  ($12.99)</p>
     <button type="submit" display:block" id='s1'>Pay 12.99 now </button>
     <button type="submit" display:block" id='s2'>Submit</button>
 </form>

simply:简单地:

no need to have several buttons doing the same thing, you can directly change the text不需要几个按钮做同样的事情,你可以直接改变文本

no need to repeat the required attribute on all radio buttons just on the first one is sufficient无需在第一个单选按钮上重复所有单选按钮所需的属性就足够了

use the labels for the radio inputs, this makes them active to act on the radio, it's the same for the checkboxes使用无线电输入的标签,这使它们可以在无线电上起作用,复选框也是如此

all the elements of a form have a hierarchical link with their form, use their name, as in a json system to access it directly, without needing to use a direct identifier (as here for myForm.btSubmit and myForm.plan )表单的所有元素都与其表单具有层次结构链接,使用它们的名称,如在 json 系统中直接访问它,无需使用直接标识符(如此处的 myForm.btSubmit 和 myForm.plan )

 const myForm = document.getElementById('my-form'), day7 = '7days' myForm.oninput = evt => { if (evt.target.name == 'plan') { myForm.btSubmit.removeAttribute('style') myForm.btSubmit.textContent = (myForm.plan.value === day7)? 'Pay 12.99 now': 'Submit' } } myForm.onsubmit = evt => { evt.preventDefault() // disable submit?for testing if (myForm.plan.value === day7 ) { // do special stuf for this case console.clear() console.log('7 days case') } else { // do normal stuff for other case console.clear() console.log('not 7 days case') } }
 label, button { display: block; float: left; clear: both; margin: .5em; }
 <form method="POST" id="my-form" > <label> <input type="radio" name="plan" value="1days" required > 1 day (free) &nbsp; </label> <label> <input type="radio" name="plan" value="7days" > 7 days ($12.99)</label> <button type="submit" name="btSubmit" style="visibility: hidden;">Submit</button> </form>

here is the answer: you can use "checked" selector in css这是答案:您可以在 css 中使用“已检查”选择器

<form method=POST>
    <input class="free-option" type="radio" name="plan" value="1days" required="required"> 1 day (free) &nbsp;
    <input class="paid-option" type="radio" name="plan" value="7days" required="required"> 7 days  ($12.99)</p>
     // if radio value == "7days":
     <button class="paid-btn" type="submit" display:block">Pay 12.99 now </button>
     // else:
     <button class="free-btn" type="submit" display:block">Submit</button>
 </form>

<style>
.free-option:checked, .free-btn {
   display: inline;
}
.paid-option:checked, .paid-btn {
   display: inline;
}
}
</style>

when ".free-option" checked, apply styles on ".free-btn" and so on.检查“ .free -option”时,请应用styles ON“ .FREE-BTN”等等。

The most sensible approach – albeit using plain JavaScript in place of jQuery – is the following.最明智的方法——尽管使用普通的 JavaScript 代替 jQuery——如下。 Please note that I've added a data-plan custom data-* attribute to each of the <button> elements which is equal to the value of the relevant <input> elements.请注意,我为每个<button>元素添加了一个data-plan自定义data-*属性,该属性等于相关<input>元素的value

I've also wrapped the <input> elements in a <label> , in order to associate the explanatory text with the relevant <input> , which allows clicking on the text to check the relevant <input> :我还将<input>元素包装在<label>中,以便将说明性文本与相关<input>相关联,这允许单击文本以检查相关<input>

 // a named function to handle the toggling, we're // using an Arrow function (since we have no need to use // or update the 'this' within the function), and passing // in the Event Object, which is supplied automatically // by EventTarget.addEventListener() method: const toggleButton = (event) => { // the <input> that fires the 'change' event (see the // EventTarget.addEventListener() call later) is the // event.target, so here we get a reference to that // changed <input>: let changed = event.target, // interactive form elements (<input>, <select>, // <button>...) all have a form property which // identifies the <form> element they belong to; // we use that to find all <button> elements // within the same <form>: buttons = changed.form.querySelectorAll('button'); // we then iterate over that NodeList using // NodeList.prototype.forEach(), using an anonymous // (unnamed) Arrow function expression: buttons.forEach( // the 'button' argument is a reference to the current // Node of the NodeList over which we're iterating; // here we use a conditional operator (ternary) to // update the display of the current <button> node. // If the buttons 'data-plan' attribute is exactly // equal to the changed <input> element's 'value' // property/attribute then we set the display to // 'initial', otherwise if not an exact match we // return 'none': (button) => button.style.display = button.dataset.plan === changed.value? 'initial': 'none' ); } // here we bind an anonymous function to the 'DOMContentLoaded' // event of the Window Object: window.addEventListener('DOMContentLoaded', () => { // we get the <form> element: const form = document.querySelector('form'), // we retrieve the <input type="radio"> elements // within the <form>: radios = form.querySelectorAll('input[type=radio]'); // and use an anonymous arrow function as the callback, // here we iterate over each <input> and bind the // toggleButton() function (note the deliberate lack of // parentheses) as the 'change' event-handler: radios.forEach( (el) => el.addEventListener('change', toggleButton) ); });
 *, ::before, ::after { box-sizing: border-box; font-size: 1rem; line-height: 1.5; margin: 0; padding: 0; } label { display: block; cursor: pointer; } button { display: none; }
 <form method="POST"> <.-- wrapping each <input> with a <label> to improve user experience --> <label> <input type="radio" name="plan" value="1days" required="required"> 1 day (free) </label> <label> <input type="radio" name="plan" value="7days" required="required"> 7 days ($12;99) </label> <:-- if radio value == "7days". also we add the custom data-plan attribute to reflect the value of the relevant <input>: --> <button data-plan="7days" type="submit">Pay 12.99 now </button> <!-- else: --> <button data-plan="1days" type="submit">Submit</button> </form>

you can do like this你可以这样做

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <form method=POST>
        <input type="radio" id="radio-seven" name="plan" value="7days" required="required"> 7 days  (£12.99)
        <input type="radio" id="radio-one" name="plan" value="1days" required="required"> 1 day (free) &nbsp;
        <input type="submit" id="btn" value="submit" class="link button" display:block">
     </form>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
     <script>
         $('#radio-one').click(()=>{
            // $('#btn').prop('value',$('#radio-one').val())
            /* or like this */
            $('#btn').prop('value','Submit')
         })
         $('#radio-seven').click(()=>{
            // $('#btn').prop('value',$('#radio-seven').val())
            /* or like this */
            $('#btn').prop('value','Pay 12.99 now')
         })
     </script>

</body>
</html>

i have used Jquery-library:)我用过 Jquery 库:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM