按钮从3条件启用
Button enabled from 3 condition
我试图让按钮只有在满足所有三个条件时才被启用,这三个条件是在第一个复选框列表中至少选择了一个复选框,第二个复选框被选中,选项列表被选中。
对于第一个条件,我想作为一种替代方案,javascript是否能够检查文本框的strlen?
不知怎的,下面的纯javascript不起作用,如果用户的选择相反,这可能吗?
纯javascript:
<script type = "text/javascript">
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('termsChkbx').addEventListener("change", function(){
this.parentNode.style.color = this.checked ? "black" : "red";
}, false);
});
function change(obj) {
var selectBox = obj;
var selected = selectBox.options[selectBox.selectedIndex].value;
var retCustDetails = document.getElementById("retCustDetails");
var tradeCustDetails = document.getElementById("tradeCustDetails");
if(selected === 'ret'){
retCustDetails.style.display = "block";
tradeCustDetails.style.display = "none";
}
else if (selected === 'trd') {
retCustDetails.style.display = "none";
tradeCustDetails.style.display = "block";
}
else if (selected === '') {
retCustDetails.style.display = "none";
tradeCustDetails.style.display = "none";
}
}
function isChecked() {
var sum = 0; //store a running sum
//find all price elements: class "CDPrice" within element of class "item"
[].forEach.call(document.querySelectorAll(".item .CDPrice"), function(item) {
//get the corresponding checkbox
var chosen = item.parentElement.querySelector('[type="checkbox"]');
//if checked, add to the running sum
if (chosen.checked) {
var value = parseFloat(item.innerHTML) || 0; //if parseFloat() returns none, default value to zero
sum += value;
}
});
//update the total
var total = document.getElementById("total");
total.value = sum.toFixed(2);
}
function Checked() {
var checkedRadioButtons = document.querySelector('[name="deliveryType"]:checked');
document.getElementById("total").value = checkedRadioButtons.getAttribute("title");
}
//conditions for submit button to be enable
//var firstCondition = document.querySelectorAll('name=CDPrice');
//var termsCheckbox = document.getElementById('termsChkbx');
var show = document.getElementById('show');
var button = document.getElementById('sub1');
var conditions = {
// cond1: false,
// cond2: false,
cond3: false
};
//function setCondition1(e) {
// conditions.cond1 = e.target.checked;
// enableButton(conditions);
//}
//function setCondition2(e) {
// conditions.cond2 = e.target.checked;
// enableButton(conditions);
//}
function setCondition3(e) {
conditions.cond3 = e.target.value && e.target.value.length > 0;
enableButton(conditions);
}
function enableButton(options) {
if (options.cond3) {
button.removeAttribute('disabled');
} else {
button.setAttribute('disabled', true);
}
}
//for(i=0 ; i< firstCondition.length ; i++){
// firstCondition[i].addEventListener("click", setCondition1, false);
//}
//termsCheckbox.addEventListener('change', setCondition2, false);
show.addEventListener('change', setCondition3, false);
</script>
第一个条件->复选框列表或文本框:
<?php
include_once('database_conn.php');
$sqlCDs = 'SELECT CDID, CDTitle, CDYear, catDesc, CDPrice FROM nmc_cd b inner join nmc_category c on b.catID = c.catID WHERE 1 order by CDTitle';
$rsCDs = mysqli_query($conn, $sqlCDs);
while ($CD = mysqli_fetch_assoc($rsCDs)) {
echo "'t<div class='item'>
<span class='CDTitle'>{$CD['CDTitle']}</span>
<span class='CDYear'>{$CD['CDYear']}</span>
<span class='catDesc'>{$CD['catDesc']}</span>
<span class='CDPrice'>{$CD['CDPrice']}</span>
<span class='chosen'><input type='checkbox' name='CD[]' value='{$CD['CDID']}' title='{$CD['CDPrice']}'onchange='isChecked();'/></span>
</div>'n";
}
?>
<section id="checkCost">
<h2>Total cost</h2>
Total <input type="text" name="total" id="total" size="10" readonly="readonly" />
</section>
第二个条件->第二个复选框:
<p style="color: red; font-weight: bold;">I have read and agree to the terms and conditions
<input type="checkbox" id="termsChkbx" onchange="isChecked(this,'sub1')"/></p>
第三种条件->选项列表:
<section id="placeOrder">
<h2>Place order</h2>
Your details
Customer Type: <select id="show" name="customerType" onchange="change(this)">
<option value="">Customer Type?</option>
<option value="ret">Customer</option>
<option value="trd">Trade</option>
</select>
提交按钮:
<p><input type="submit" name="submit" value="Order now!" id="sub1" disabled="disabled"/></p>
您可以在每个输入上附加事件侦听器并侦听更改。
var firstCondition = document.querySelectorAll('input[name="testName"]');
var termsCheckbox = document.getElementById('termsChkbx');
var show = document.getElementById('show');
var button = document.getElementById('sub1');
持有所有条件的对象
var conditions = {
cond1: false,
cond2: false,
cond3: false
}
要与addEventListener 一起使用的声明函数
function setCondition1(e) {
conditions.cond1 = e.target.checked;
enableButton(conditions);
}
function setCondition2(e) {
conditions.cond2 = e.target.checked;
enableButton(conditions);
}
function setCondition3(e) {
conditions.cond3 = e.target.value && e.target.value.length > 0;
enableButton(conditions);
}
启用按钮
function enableButton(options) {
if (options.cond1 && options.cond2 && options.cond3) {
button.removeAttribute('disabled');
} else {
button.setAttribute('disabled', true);
}
}
添加事件侦听器
for(i=0 ; i< firstCondition.length ; i++){
firstCondition[i].addEventListener("click", setCondition1, false);
}
termsCheckbox.addEventListener('change', setCondition2, false);
show.addEventListener('change', setCondition3, false);
示例:http://jsfiddle.net/qjo9rqgc/
相关文章:
- 淘汰赛.JS'启用'长度绑定条件不起作用
- 根据接受2个复选框条件启用/禁用按钮
- 如何根据条件启用或禁用脚本
- 在条件jquery上启用按钮/输入区域
- 根据同一页面上的 if 条件禁用/启用 PHP 中的表单值
- 根据客户端变量的状态有条件地启用/禁用 @Html.DropDownListFor
- jQuery - 根据条件禁用和启用锚标记
- 根据无线电输入条件禁用和启用按钮提交
- 启动日期选择器在条件下禁用和启用
- g: submitButton条件启用/禁用
- 按钮从3条件启用
- 这可能在javascript中(不使用jquery)使键盘的tab键在if-else条件下启用或禁用吗
- 可以禁用表中的所有字段,并添加条件以启用需要的某些字段
- 如何启用/禁用提交按钮,仅当提供两个条件时:检查输入单选和填充输入文本
- 如何根据某些定义的条件启用和禁用提交按钮
- 通过jquery中的复选框条件来启用和禁用按钮
- 如何在URL满足某些条件时才启用Safari工具栏按钮扩展('validate'检查)
- 使用gulp-if(或其他东西)有条件地启用gulp-watch
- 满足两个条件时启用按钮
- 根据某些条件禁用或启用按钮