强制不可见文本字段具有值“0”。或“”
我知道这有点笨拙,但我已经非常接近解决方案了,所以我不得不发布这个。
我有一个带有两个复选框的表单,可以切换相关跨度是否可见。 num_bw 和 num_c 是包含输入框的范围,用户可以在其中输入用户想要的两种类型产品的数量。不过,我担心用户可能会在暂时可见的文本字段中输入一个数字(例如第一个,在 num_bw 范围内),但随后会改变主意并取消选中该复选框并在框中输入一个数字相反,在 num_c 范围内。
这段代码的工作方式是,num_bw 字段现在是不可见的,但它可能仍然包含一个数字!如果他点击提交,用户可能会惊讶地发现那 3 个黑色和白色。他不再想要的白色产品仍在他的购物车中!哎呀!
因此,我需要检查这两个文本字段中的任何一个(它们的名称是 orderquantitybw 和 orderquantityc,如果需要,我还可以添加 ID)位于现在不可见的范围内(#num_bw 和 #num_c),以及它们是否位于,将它们的价值变成零,零,零,“”,任何不会混淆购物车软件的东西(这不是我的部门,它的工作原理对我来说有点不透明。)
这是在头部,它只是使这些跨度在开始时不可见:
$(document).ready(function() {
$('#num_bw').hide();
$('#num_c').hide();
});
这就在相关表单之后,当用户单击复选框时,它使跨度可见或不可见:
$('#show_c').click(function(){
$('#num_c').toggle();
});
$('#show_bw').click(function(){
$('#num_bw').toggle();
});
我想在末尾添加一个函数。还没写完我就停了下来。有什么帮助吗?
$('#submit_btn').click(function(){
/*Insert script to turn form box's values to "0" if
they are in an invisible span*/
)};
I know this is kind of cludgey, but I am so close to a solution that I had to post this.
I have a form with two checkboxes that toggle whether or not the relevant spans are visible. num_bw and num_c are spans that contain input boxes where the user puts how many of the two types of products the user wants. I am worried, though, that the user might put a number into a text field that's temporarily visible (say the first one, in the num_bw span), but then would change his mind and uncheck that check box and put a number into the box in the num_c span instead.
The way this code works, the num_bw field would now be invisible- but it could still contain a number! If he hits submit, the user might be surprised to see that those 3 black & white products he no longer wants are still in his shopping cart! Woops!
So I need to check if any of these two text fields (their names are orderquantitybw and orderquantityc, I can also add ID's if I need to) are in a span which is now invisible (#num_bw and #num_c), and if they are, to turn their value to nothing, zero, zilch, "", whatever won't confuse the shopping cart software (which isn't my department, how it works is a tad opaque to me.)
This is in the head, it just makes these spans invisible at the start:
$(document).ready(function() {
$('#num_bw').hide();
$('#num_c').hide();
});
This is right after the form in question, it makes the spans visible or invisible when the user clicks on the checkboxes:
$('#show_c').click(function(){
$('#num_c').toggle();
});
$('#show_bw').click(function(){
$('#num_bw').toggle();
});
And I want to add a function to the end. I stopped just short of writing it. Any help?
$('#submit_btn').click(function(){
/*Insert script to turn form box's values to "0" if
they are in an invisible span*/
)};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
应该这样做。或者你可以这样做:
对于类为“quantityTextBox”的文本框
should do it. Or you could do something like this:
for textboxes with the class "quantityTextBox"