Javascript 我不能在jQuery中使用两个条件
我有一张这样的表格:Javascript 我不能在jQuery中使用两个条件,javascript,jquery,html,forms,Javascript,Jquery,Html,Forms,我有一张这样的表格: <form> <input type="radio" name="adsType" id="adsType" value="request" /> <input type="radio" name="adsType" id="adsType" value="provide" /> <select name="transactionType" id="transactionType"> <option valu
<form>
<input type="radio" name="adsType" id="adsType" value="request" />
<input type="radio" name="adsType" id="adsType" value="provide" />
<select name="transactionType" id="transactionType">
<option value="1">Sale</option>
<option value="2">Rent</option>
</select>
<input type="text" name="price" id="price" />
<input type="text" name="min_price" id="price" />
<input type="text" name="max_price" id="max_price" />
<input type="text" name="fare" id="fare" />
<input type="text" name="min_fare" id="min_fare" />
<input type="text" name="max_fare" id="max_fare" />
</form>
但它并没有收到好的效果。请帮我编辑这个JavaScript代码 试试看:
$(document).ready(function () {
var radioValue = "request";
var transactionValue = 1;
$('input[name=adsType]').change(function () {
radioValue = $(this).val();
update();
});
$('select[name=transactionType]').change(function () {
transactionValue = parseInt($(this).val(), 10);
update();
});
function update() {
if (radioValue === "provide") {
if (transactionValue === 1) {
$('#price').show();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').hide();
$('#min_fare').hide();
$('#max_fare').hide();
} else {
$('#price').hide();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').show();
$('#min_fare').hide();
$('#max_fare').hide();
}
} else {
if (transactionValue === 1) {
$('#price').hide();
$('#min_price').show();
$('#max_price').show();
$('#fare').hide();
$('#min_fare').hide();
$('#max_fare').hide();
} else {
$('#price').hide();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').hide();
$('#min_fare').show();
$('#max_fare').show();
}
}
}
update();
});
表单HTML>请检查表单id:min_price
<form>
<input type="radio" name="adsType" id="adsType" value="request" checked="checked" />
<input type="radio" name="adsType" id="adsType" value="provide" />
<select name="transactionType" id="transactionType">
<option value="1" selected="selected">Sale</option>
<option value="2">Rent</option>
</select>
<input type="text" name="price" id="price" />
<input type="text" name="min_price" id="min_price" />
<input type="text" name="max_price" id="max_price" />
<input type="text" name="fare" id="fare" />
<input type="text" name="min_fare" id="min_fare" />
<input type="text" name="max_fare" id="max_fare" />
</form>
特价
租
试试看:
$(document).ready(function () {
var radioValue = "request";
var transactionValue = 1;
$('input[name=adsType]').change(function () {
radioValue = $(this).val();
update();
});
$('select[name=transactionType]').change(function () {
transactionValue = parseInt($(this).val(), 10);
update();
});
function update() {
if (radioValue === "provide") {
if (transactionValue === 1) {
$('#price').show();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').hide();
$('#min_fare').hide();
$('#max_fare').hide();
} else {
$('#price').hide();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').show();
$('#min_fare').hide();
$('#max_fare').hide();
}
} else {
if (transactionValue === 1) {
$('#price').hide();
$('#min_price').show();
$('#max_price').show();
$('#fare').hide();
$('#min_fare').hide();
$('#max_fare').hide();
} else {
$('#price').hide();
$('#min_price').hide();
$('#max_price').hide();
$('#fare').hide();
$('#min_fare').show();
$('#max_fare').show();
}
}
}
update();
});
表单HTML>请检查表单id:min_price
<form>
<input type="radio" name="adsType" id="adsType" value="request" checked="checked" />
<input type="radio" name="adsType" id="adsType" value="provide" />
<select name="transactionType" id="transactionType">
<option value="1" selected="selected">Sale</option>
<option value="2">Rent</option>
</select>
<input type="text" name="price" id="price" />
<input type="text" name="min_price" id="min_price" />
<input type="text" name="max_price" id="max_price" />
<input type="text" name="fare" id="fare" />
<input type="text" name="min_fare" id="min_fare" />
<input type="text" name="max_fare" id="max_fare" />
</form>
特价
租
在代码的if($(this).val()=='request'){
中,选择框没有onchange事件
我做了一些更改,比如在输入中添加classall
您可以浏览下面代码段中的代码
//仅显示“价格”输入*/
$('价格').show();
}
if($(this).val()='2'){/*提供类型|租金类型=>仅显示“票价”输入*/
$('票价').show();
}
});
}
if($(this).val()=='request'){
$('select[name=transactionType]')。更改(函数(){
$('.all').hide();
如果($(this).val()='1'){/*请求类型|销售类型=>显示“最低价格”和“最高价格”输入*/
$(“#最低价格,#最高价格”).show();
}
if($(this).val()='2'){/*请求类型|租金类型=>显示“最低票价”和“最高票价”输入*/
$(“#最低票价,#最高票价”).show();
}
});
};
});
});代码>
。全部{
显示:无;
}
请求提供
特价
租
在代码的if($(this).val()=='request'){
中,选择框没有onchange事件
我做了一些更改,比如在输入中添加classall
您可以浏览下面代码段中的代码
//仅显示“价格”输入*/
$('价格').show();
}
if($(this).val()='2'){/*提供类型|租金类型=>仅显示“票价”输入*/
$('票价').show();
}
});
}
if($(this).val()=='request'){
$('select[name=transactionType]')。更改(函数(){
$('.all').hide();
如果($(this).val()='1'){/*请求类型|销售类型=>显示“最低价格”和“最高价格”输入*/
$(“#最低价格,#最高价格”).show();
}
if($(this).val()='2'){/*请求类型|租金类型=>显示“最低票价”和“最高票价”输入*/
$(“#最低票价,#最高票价”).show();
}
});
};
});
});代码>
。全部{
显示:无;
}
请求提供
特价
租
我相信这样做可能会有所帮助。请参见代码中的代码段和注释:
$(函数(){
函数toggleFields(){
//设置每种情况下要显示的字段
可变项目={
提供:{
1:"价格",,
2:"票价"
},
请求:{
1:“#最低价格,#最高价格”,
2:“#最低票价,#最高票价”
}
}
//获取选定的值
var ad=$('input[name=“adsType”]:checked').val(),
tr=$('select[name=“transactionType”]”)。val();
//隐藏所有内容并显示所需内容
$('[id*=“price”],[id*=“fare”]')。hide();
$(items[ad][tr])。show();
}
//更改处理程序
$('select[name=“transactionType”],input[name=“adsType”]')。更改(切换字段);
//初次运行
切换字段();
});代码>
要求
供给
特价
租
价格
最低价格
最高价格
票价
最低票价
最高票价
我相信这样做可能会有所帮助。请参见代码中的代码段和注释:
$(函数(){
函数toggleFields(){
//设置每种情况下要显示的字段
可变项目={
提供:{
1:"价格",,
2:"票价"
},
请求:{
1:“#最低价格,#最高价格”,
2:“#最低票价,#最高票价”
}
}
//获取选定的值
var ad=$('input[name=“adsType”]:checked').val(),
tr=$('select[name=“transactionType”]”)。val();
//隐藏所有内容并显示所需内容
$('[id*=“price”],[id*=“fare”]')。hide();
$(items[ad][tr])。show();
}
//更改处理程序
$('select[name=“transactionType”],input[name=“adsType”]')。更改(切换字段);
//初次运行
切换字段();
});代码>
要求
供给
特价
租
价格
最低价格
最高价格
票价
最低票价
最高票价
你应该解释为什么你的代码能工作,为什么OP不能工作。你的代码在绑定$('select[name=transactionType]')。change()时,该值一直在变化。您应该只绑定事件1次。你应该解释为什么你的代码可以工作,为什么OP不能工作。你的代码在绑定$('select[name=transactionType]').change()值的所有时间都在更改。您应该只绑定事件1次。你展示而不是隐藏别人。