我有一个onChange函数,该函数可在我创建的预订表单上使用,但是当我创建的更新表单加载后,我无法找到一种过滤方式,就好像它已被选中一样,下面是所用代码的摘要对每个被过滤的元素重复;
<input name="CQty" id="CQty" value="<?php echo $noc; ?>" onChange="if (this.value=='1'){this.form['C1N'].style.visibility='visible'}"
到目前为止,这里是我尝试进行jquery的地方(尽管与最终要求的长度相比,它缩短了);
document.onload = function clientQty() {
var Qty = document.update.CQty;
if (['#CQty'].value=='1'){
$['#C1N'].style.visibility='visible'
$['#C1Al'].style.visibility='visible'
$['#C1Gen'].style.visibility='visible'
$['#C1DOB'].style.visibility='visible'
$['#C1Age'].style.visibility='visible'
$['#C1Rel'].style.visibility='visible'
$['#C1IDNo'].style.visibility='visible'
$['#C1Photo'].style.visibility='visible'
$['#C1PoC'].style.visibility='visible'
$['#C1PKB'].style.visibility='visible'
$['#C1Res'].style.visibility='visible'
$['#C2N'].style.visibility='hidden'
$['#C2Al'].style.visibility='hidden'
$['#C2Gen'].style.visibility='hidden'
$['#C2DOB'].style.visibility='hidden'
$['#C2Age'].style.visibility='hidden'
$['#C2Rel'].style.visibility='hidden'
}
else if (['#CQty'].value=='10'){
$['#C1N'].style.visibility='visible'
$['#C1Al'].style.visibility='visible'
$['#C1Gen'].style.visibility='visible'
$['#C1DOB'].style.visibility='visible'
$['#C1Age'].style.visibility='visible'
$['#C1Rel'].style.visibility='visible'
$['#C1IDNo'].style.visibility='visible'
$['#C1Photo'].style.visibility='visible'
$['#C1PoC'].style.visibility='visible'
$['#C1PKB'].style.visibility='visible'
$['#C1Res'].style.visibility='visible'
$['#C2N'].style.visibility='visible'
$['#C2Al'].style.visibility='visible'
$['#C2Gen'].style.visibility='visible'
$['#C2DOB'].style.visibility='visible'
$['#C2Age'].style.visibility='visible'
$['#C2Rel'].style.visibility='visible'
}
};
任何想法都会受到赞赏,因为这是我现在唯一坚持的事情。
答案 0 :(得分:0)
window.onload = function() {
if (document.getElementById("number").value=='1'){
this.form['C1N'].style.visibility='visible'
}
};
尝试一下。
答案 1 :(得分:0)
我没有您的PHP脚本,因此要进行测试,我更改了HTML,因此输入值默认为1
,如下所示:
<input name="CQty" id="CQty" value="1" onChange="if (this.value=='1'){this.form['C1N'].style.visibility='visible'}">
您的JavaScript对此:
window.onload = clientQty;
function clientQty() {
console.log($('#CQty').attr('value')) //1
//OR
console.log(document.getElementById('CQty').value) //1
//change all of these selectors so they work like the ones in the console.log statement
};
您的选择器无法正常工作。如果您使用的是jQuery选择器(即$
),则无法像编写.style.visibility = 'visible'
时那样引用JavaScript中的常规DOM元素,而必须使用jQuery。就像我在上面的$('#CQty').attr('value')
中所做的那样。反之亦然-不能在jQuery中使用JavaScript DOM元素,除非将它们变成jQuery对象。
为此,我建议您查看控制台(Google如何在您选择的浏览器中进行操作),并确保在页面加载时为这些console.log
语句提供一个值。如果该值未定义,则您的if
语句将永远不会为真,因为undefined
不等于1
。
最后一条笔记-了解JavaScript中this
的含义。在onChange="if (this.value=='1'){this.form['C1N'].style.visibility='visible'}"
行中,您说的是this.form
,但是在这种情况下,this
是输入,我想它没有表单属性。