Javascript 当单选值=”时,jQuery识别页面加载上单选按钮的不正确状态;否";并且没有选择任何一个收音机
我在页面上有单选按钮,可以根据其他元素的状态添加或删除css类 默认情况下,类Javascript 当单选值=”时,jQuery识别页面加载上单选按钮的不正确状态;否";并且没有选择任何一个收音机,javascript,jquery,radio-button,Javascript,Jquery,Radio Button,我在页面上有单选按钮,可以根据其他元素的状态添加或删除css类 默认情况下,类testclass添加到元素slave(即页面加载的HTML为: <input type="radio" value="No" name="master" /> No <input type="radio" value="Yes" name="master" /> Yes <input type="text" name="a" id="slave" class="testclass" /
testclass
添加到元素slave
(即页面加载的HTML为:
<input type="radio" value="No" name="master" /> No
<input type="radio" value="Yes" name="master" /> Yes
<input type="text" name="a" id="slave" class="testclass" />
这在页面加载后在状态之间来回移动方面很好,但我在页面加载时触发函数,当未选择check radio时,会删除该类(如果选择“是”加载为选中状态,则可以)
有趣的是,如果我颠倒条件,要求“是”来删除类(页面加载时类仍在那里),这也没关系。
我认为javascript可能将不选择等同于值“no”,因此我尝试了严格的相等,但没有区别
有什么想法吗?之所以发生这种情况,是因为您尚未在页面加载上选择任何值,但仍在触发事件 尝试: 默认情况下,选中任何复选框
<input type="radio" value="No" name="radios" checked="checked"/> No
之所以发生这种情况,是因为您尚未在页面加载上选择任何值,但仍在触发事件 尝试: 默认情况下,选中任何复选框
<input type="radio" value="No" name="radios" checked="checked"/> No
之所以发生这种情况,是因为您尚未在页面加载上选择任何值,但仍在触发事件 尝试: 默认情况下,选中任何复选框
<input type="radio" value="No" name="radios" checked="checked"/> No
之所以发生这种情况,是因为您尚未在页面加载上选择任何值,但仍在触发事件 尝试: 默认情况下,选中任何复选框
<input type="radio" value="No" name="radios" checked="checked"/> No
我已经开发了一个小提琴供你考虑。下面是它的链接=>/p> 我认为回答这个问题的最好方法是让您理解关于您在这里提供的代码的一些事情。我将在这里写一些注释
$( document ).ready(function(){ // this function is called once your page loads
$("input[name$='master']").change(function(){
//this function is not called until you click the radio buttons.
// Be careful to note that this function is never called on page load.
// You can test it in the fiddle I created
if(this.value === 'No') {
$('#slave').removeClass('testclass');
// this alert in the fiddle is called only
// when you click the radio buttons. Not on page load
alert("It entered");
} else {
$('#slave').addClass('testclass');
}
});
$("input[name$='master']").filter(':checked').change()
});
因此,请确保要在页面加载时执行的内容和检查不在$(“input[name$='master']”)的范围内。更改(function(){});
<>这只是在点击单选按钮而不是页面加载时触发的。 < P>我已经开发了一个供您考虑的小提琴。这里是它的链接=>/P> 我认为回答这个问题的最好方法是让您理解关于您在这里提供的代码的一些事情。我将在这里写一些注释
$( document ).ready(function(){ // this function is called once your page loads
$("input[name$='master']").change(function(){
//this function is not called until you click the radio buttons.
// Be careful to note that this function is never called on page load.
// You can test it in the fiddle I created
if(this.value === 'No') {
$('#slave').removeClass('testclass');
// this alert in the fiddle is called only
// when you click the radio buttons. Not on page load
alert("It entered");
} else {
$('#slave').addClass('testclass');
}
});
$("input[name$='master']").filter(':checked').change()
});
因此,请确保要在页面加载时执行的内容和检查不在$(“input[name$='master']”)的范围内。更改(function(){});
<>这只是在点击单选按钮而不是页面加载时触发的。 < P>我已经开发了一个供您考虑的小提琴。这里是它的链接=>/P> 我认为回答这个问题的最好方法是让您理解关于您在这里提供的代码的一些事情。我将在这里写一些注释
$( document ).ready(function(){ // this function is called once your page loads
$("input[name$='master']").change(function(){
//this function is not called until you click the radio buttons.
// Be careful to note that this function is never called on page load.
// You can test it in the fiddle I created
if(this.value === 'No') {
$('#slave').removeClass('testclass');
// this alert in the fiddle is called only
// when you click the radio buttons. Not on page load
alert("It entered");
} else {
$('#slave').addClass('testclass');
}
});
$("input[name$='master']").filter(':checked').change()
});
因此,请确保要在页面加载时执行的内容和检查不在$(“input[name$='master']”)的范围内。更改(function(){});
<>这只是在点击单选按钮而不是页面加载时触发的。 < P>我已经开发了一个供您考虑的小提琴。这里是它的链接=>/P> 我认为回答这个问题的最好方法是让您理解关于您在这里提供的代码的一些事情。我将在这里写一些注释
$( document ).ready(function(){ // this function is called once your page loads
$("input[name$='master']").change(function(){
//this function is not called until you click the radio buttons.
// Be careful to note that this function is never called on page load.
// You can test it in the fiddle I created
if(this.value === 'No') {
$('#slave').removeClass('testclass');
// this alert in the fiddle is called only
// when you click the radio buttons. Not on page load
alert("It entered");
} else {
$('#slave').addClass('testclass');
}
});
$("input[name$='master']").filter(':checked').change()
});
因此,请确保要在页面加载时执行的内容和检查不在$(“input[name$='master']”)的范围内。更改(function(){});
这只会在您单击单选按钮时触发,而不会在页面加载时触发。Hmm但是我需要在用户保存任何数据之前将表单设置为空白,并且在用户到达页面时,如果有状态要加载,我还需要加载页面状态。此外,如果要求为“是”,并且没有选择状态,它也可以正常工作。知道为什么为“否”是不同的吗?嗯,但是我需要在用户保存任何数据之前将表单设置为空白,并且如果有状态要加载,我还需要在用户到达时加载页面状态。另外,如果要求为“是”,并且没有选择状态,它也可以正常工作。知道为什么为“否”是不同的吗?嗯,但是我需要在用户保存任何数据之前将表单设置为空白,并且如果有状态要加载,我还需要在用户到达时加载页面状态。另外,如果要求为“是”,并且没有选择状态,它也可以正常工作。知道为什么为“否”是不同的吗?嗯,但是在用户保存任何数据之前,我需要表单为空,如果有状态要加载,我还需要在它们到达时加载页面状态。如果要求为“是”并且没有选择状态,它也可以正常工作。知道为什么“否”不同吗?$(“输入[名称$='master']”)。更改(函数(){}仅当您更改包含“master”的name属性的单选按钮的状态时,此选项才起作用。因此,显然此函数中的代码不会在pageload上触发。为什么要删除该类?是的,这看起来很奇怪。显然,只有在无线电状态发生更改时,它才应该出现在此函数中。了解发生了什么的更好方法是在“if(this.value=='No')行上方引入断点。现在,如果刷新页面,代码在调试器处停止,我们将知道函数正在pageload处被调用。当然我可以这样做-很抱歉,我应该澄清一些我意识到我没有在代码中添加的内容,函数位于
$(文档)中。ready()
,我想这就是它在页面加载时调用的原因吧?这是在页面加载时检查状态所必需的,不是吗?Naa我认为这是正常的。现在请在那里插入断点。让我用这段代码为您做一个小提琴,以便您更好地理解。然后我会将其作为答案发布。$($(“输入[名称$='master')).change(function(){}只在您更改包含“master”的name属性的单选按钮的状态后才起作用