[英]Mark radio/checkbox buttons required
I would like to add some rules to the following checkboxes:我想在以下复选框中添加一些规则:
<form action="{% url 'payment' item.slug %}" method="POST">
{% csrf_token %}
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" required name="half_price" id='c01' value="{{item.half_price}}" onclick="check(this);" style=" margin-top: 20px; "{% if item.half_price %} {% else %} disabled {% endif %} > Half: ₹ {{item.half_price}} /- </h5>
<span class="checkmark"></span>
</label>
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" required name="full_price" id='c02' value={{item.full_price}} onclick="check(this);" {% if item.full_price %} {% else %} disabled {% endif %}> Full : ₹ {{item.full_price}} /- </h5>
<span class="checkmark"></span>
</label>
<input style="margin-left: 10px;" type="submit" class="button" value="ADD">
</form>
Of note, I have jQuery in which restricts the user to only select one checkbox.值得注意的是,我有 jQuery ,其中将用户限制为仅 select 一个复选框。
You can write a function on input click to check you checkbox validation (If you don't want to change your html).您可以在输入单击时编写 function 以检查复选框验证(如果您不想更改 html)。
var validateBeforePost = function(){ var chkCount = document.querySelectorAll('input.radioCheck[type="checkbox"]:checked'); if(chkCount.length == 0 || chkCount.length > 1 ){ alert('Select single valid price'); return false; } return true; } //check function check(){}
<form> <label class="radio-inline"> <h5> <input type="checkbox" class="radioCheck" name="half_price" id='c01' value="{{item.half_price}}" onclick="check(this);" style=" margin-top: 20px; "> Half: ₹ {{item.half_price}} /- </h5> <span class="checkmark"></span> </label> <label class="radio-inline"> <h5> <input type="checkbox" class="radioCheck" name="full_price" id='c02' value={{item.full_price}} onclick="check(this);" > Full: ₹ {{item.full_price}} /- </h5> <span class="checkmark"></span> </label> <input style="margin-left: 10px;" type="submit" class="button" onclick="return validateBeforePost();" value="ADD"> </form>
Why not make it required only if the half_price
is available.为什么不只在
half_price
可用时才要求它。
<input type="checkbox" name="half_price" value="{{item.half_price}}"
{% if item.half_price %} required {% else %} disabled {% endif %}
>
Similarly for full_price
同样对于
full_price
<input type="checkbox" name="full_price" value="{{item.full_price}}"
{% if item.full_price %} required {% else %} disabled {% endif %}
>
EDIT:编辑:
Changes need to be done.需要进行更改。
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="half_price" id='c01' value="{{item.half_price}}" onclick="check(this);" style="margin-top: 20px;" {% if item.half_price %}required{% else %}disabled{% endif %} > Half: ₹ {{item.half_price}}/- </h5>
<span class="checkmark"></span>
</label>
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="full_price" id='c02' value={{item.full_price}} onclick="check(this);" {% if item.full_price %}required{% else %}disabled{% endif %} > Full : ₹ {{item.full_price}}/- </h5>
<span class="checkmark"></span>
</label>
<input style="margin-left: 10px;" type="submit" class="button" value="ADD">
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="half_price" id='c01' value="40" onclick="check(this);" style="margin-top: 20px;" required > Half: ₹ 40/- </h5>
<span class="checkmark"></span>
</label>
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="full_price" id='c02' value=80 onclick="check(this);" required > Full : ₹ 80/- </h5>
<span class="checkmark"></span>
</label>
<input style="margin-left: 10px;" type="submit" class="button" value="ADD">
Will translate to将翻译为
{
item: {
half_price: 40
}
}
{
item: {}
}
will translate to将转化为
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="half_price" id='c01' value="" onclick="check(this);" style="margin-top: 20px;" disabled > Half: ₹ /- </h5>
<span class="checkmark"></span>
</label>
<label class="radio-inline">
<h5> <input type="checkbox" class="radioCheck" name="full_price" id='c02' value= onclick="check(this);" disabled > Full : ₹ /- </h5>
<span class="checkmark"></span>
</label>
<input style="margin-left: 10px;" type="submit" class="button" value="ADD">
{ item: {} }
Will translate to将翻译为
<label class="radio-inline"> <h5> <input type="checkbox" class="radioCheck" name="half_price" id='c01' value="" onclick="check(this);" style="margin-top: 20px;" disabled > Half: ₹ /- </h5> <span class="checkmark"></span> </label> <label class="radio-inline"> <h5> <input type="checkbox" class="radioCheck" name="full_price" id='c02' value= onclick="check(this);" disabled > Full: ₹ /- </h5> <span class="checkmark"></span> </label> <input style="margin-left: 10px;" type="submit" class="button" value="ADD">
UPDATE: Resolving the question in comments.更新:解决评论中的问题。 If you only want the values, then these are the changes need to be done.
如果您只想要这些值,那么这些就是需要进行的更改。
<label class="radio-inline"> <h5> <input type="radio" class="radioCheck" name="price" id='c01' value="{{item.half_price}}" onclick="check(this);" style="margin-top: 20px;" {% if item.half_price %}required{% else %}disabled{% endif %} > Half: ₹ {{item.half_price}}/- </h5> <span class="checkmark"></span> </label> <label class="radio-inline"> <h5> <input type="radio" class="radioCheck" name="price" id='c02' value={{item.full_price}} onclick="check(this);" {% if item.full_price %}required{% else %}disabled{% endif %} > Full: ₹ {{item.full_price}}/- </h5> <span class="checkmark"></span> </label> <input style="margin-left: 10px;" type="submit" class="button" value="ADD">
{ item: { "half_price": 80, "full_price": 160 } }
Will translate to将翻译为
<label class="radio-inline"> <h5> <input type="radio" class="radioCheck" name="price" id='c01' value="80" onclick="check(this);" style="margin-top: 20px;" required > Half: ₹ 80/- </h5> <span class="checkmark"></span> </label> <label class="radio-inline"> <h5> <input type="radio" class="radioCheck" name="price" id='c02' value=160 onclick="check(this);" required > Full: ₹ 160/- </h5> <span class="checkmark"></span> </label> <input style="margin-left: 10px;" type="submit" class="button" value="ADD">
First of, you shouldn't require jQuery to keep one checkbox selected at a time.首先,您不应该要求 jQuery 一次选中一个复选框。 Keeping same 'name' attribute for both of the checkboxes will work.
为两个复选框保持相同的“名称”属性将起作用。 When you keep the name attr same for the checkboxes then add 'checked' attribute to your 'Full Price' checbox so that on page load it is always selected.
当您保持复选框的名称属性相同时,将“已选中”属性添加到“全价”复选框,以便在页面加载时始终选中它。 User will have option to check the 'Half Price' checkbox if it was not disabled by your condition.
如果您的条件没有禁用“半价”复选框,用户可以选择选中它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.