Jquery - 开关 &&不工作?
我试图根据前面 2 个字段的值来控制隐藏的输入值。我在 switch 语句方面取得了一些成功,但我似乎无法让它准确地工作。我想要发生的是测试用户是否在 #state 字段中输入了 CA 或 NV,如果他们输入了,并且他们选择了选项值 auto,则 #ins_link 值将更改为 CA 或 NV(分别)。我在这里做错了什么?这是我正在处理的内容:
<input class="statefield isstate" name="state" id="state" maxlength="2" onkeyup="this.value=this.value.toUpperCase();">
<select class="input_field_12em required" name="ins_type" id="ins_type">
<option value="" selected="selected" >---Select Insurance Type---</option>
<option value="home">Home</option>
<option value="auto">Auto</option>
<option value="homeauto">Home + Auto</option>
<option value="renter">Renter</option>
<option value="buildingowner">Building Owner</option>
<option value="dwellingfire">Dwelling Fire</option>
<option value="commercial">Commercial</option>
<option value="agricultural">Agricultural</option>
</select>
$(function() {
$('#ins_type').change(function() {
switch (this.value) {
case 'auto':
if ($("#state[CA]" && "#ins_type[auto]"))
$(".ins_link").val("CA");
break;
case 'auto':
if ($("#state[NV]" && "#ins_type[auto]"))
$(".ins_link").val("NV");
default :
$(".ins_link").val("summary");
break;
}
});
});
<input type="hidden" class="link ins_link" value="summary">
I am trying to control a hidden input value based on the value of 2 previous fields. I've had some success with a switch statement, but I just can't seem to get it to work exactly. What I would like to have happen is to test if a user entered CA or NV in the field #state, if they did, AND they selected the option value auto, the #ins_link value will change to CA or NV(respectively). What am I doing wrong here? Here is what I am working with:
<input class="statefield isstate" name="state" id="state" maxlength="2" onkeyup="this.value=this.value.toUpperCase();">
<select class="input_field_12em required" name="ins_type" id="ins_type">
<option value="" selected="selected" >---Select Insurance Type---</option>
<option value="home">Home</option>
<option value="auto">Auto</option>
<option value="homeauto">Home + Auto</option>
<option value="renter">Renter</option>
<option value="buildingowner">Building Owner</option>
<option value="dwellingfire">Dwelling Fire</option>
<option value="commercial">Commercial</option>
<option value="agricultural">Agricultural</option>
</select>
$(function() {
$('#ins_type').change(function() {
switch (this.value) {
case 'auto':
if ($("#state[CA]" && "#ins_type[auto]"))
$(".ins_link").val("CA");
break;
case 'auto':
if ($("#state[NV]" && "#ins_type[auto]"))
$(".ins_link").val("NV");
default :
$(".ins_link").val("summary");
break;
}
});
});
<input type="hidden" class="link ins_link" value="summary">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决您的问题的实际方法是
您看到这段代码实际上如何反映您的需求了吗?
#ins_type
的值为“auto”:#state
的值为“CA”或“NV”,请将.ins_link
的值设置为所选的州。.ins_link
的值设置为“summary”。你的代码结构应该反映你的思维结构。
The actual way to solve your problem would be
Do you see how this code actually reflects your requirements?
#ins_type
's value is "auto":#state
's value is "CA" or "NV", set.ins_link
's value to the state selected..ins_link
's value to "summary".Your code structure should reflect your thought structure.
这是对两个字符串进行逻辑与,然后将其结果用作页面中元素的选择器。这是行不通的。
您的示例代码没有显示您存储状态信息的位置/方式,但您想要更多类似这样的内容:
在内部 if() 中再次检查“自动”选择是多余的,因为您已经强制执行它使用
switch()
设为“自动”。That's doing the logical AND of two strings, the result of which is then used as a selector for an element in your page. This will not work.
Your sample code doesn't show where/how you store the state information, but you'd want something more like this:
Checking for the 'auto' selection again within the inner if() is redundant, as you've already forced it to be 'auto' with the
switch()
.您对 JavaScript 和 jQuery 的工作方式存在一些误解。
"#state[CA]"
只是一个字符串,"#ins_type[auto]"
"#state[CA]" && "#ins_type[auto]"
将这些字符串转换为布尔值,然后询问它们是否均为 true。只有空字符串才会转换为 false,因此逻辑是:然后用
$
函数调用包装它,该函数调用只是将其参数转换为 jQuery 对象。也就是说,您只得到了空的 jQuery 对象,因为您没有传递选择器或 DOM 元素。
如果您使用
if
测试任何非null
、非undefined
对象,它会将其转换为 bool,因此您会得到if( true)
:相同的逻辑适用于第二个
if
语句,因此两个if
语句不执行任何操作。You have some misunderstandings of how JavaScript and jQuery work here.
"#state[CA]"
is just a string, as is"#ins_type[auto]"
"#state[CA]" && "#ins_type[auto]"
converts those strings to bools, then asks if they are both true. Only the empty string converts to false, so the logic is:Then you are wrapping that with a
$
function call, which just converts its arguments to a jQuery object. Soi.e. you just got the empty jQuery object, since you didn't pass a selector or a DOM element.
If you test any non-
null
, non-undefined
object withif
, it converts it to bool, so you getif(true)
:The same logic applies to your second
if
statement, so bothif
statements do nothing.