[英]jQuery Change radio button on select box change
I am not very good with jQuery and I am having trouble getting this right. 我对jQuery不太满意,并且在正确设置方面遇到困难。 I need to change the radio button selected if I select a certain value in a different select box.
如果我在其他选择框中选择某个值,则需要更改选中的单选按钮。
<div class="radio-inline" id="sourceDiv" role="group"> <input type="radio" id="sourceBtns1" name="sourceBtn" class="btn btn-lg" value="CN"><label for="sourceBtns1">CHINA</label> <input type="radio" id="sourceBtns2" name="sourceBtn" class="btn btn-lg" value="ID" ><label for="sourceBtns2">INDONESIA</label> <input type="radio" id="sourceBtns3" name="sourceBtn" class="btn btn-lg" value="TH" ><label for="sourceBtns3">THAILAND</label> <input type="radio" id="sourceBtns4" name="sourceBtn" class="btn btn-lg" value="US"><label for="sourceBtns4">UNITED STATES</label> </div>
<div id="priceSelect"> <select name="priceSelect" id="priceSelect" class="form-control"> <option value="FTLW">Domestic Full Truck Load</option> <option value="PTLW">Domestic Partial Truck Load</option> <option value="FTL" selected>International</option> </select> </div>
I am trying to get the radio button to select #sourceBtns4 when the "FTLW" option is selected in the select box. 当在选择框中选择“ FTLW”选项时,我试图使单选按钮选择#sourceBtns4。
This is the function that I am using. 这是我正在使用的功能。 I do not get an error, but I cannot get the function to work.
我没有收到错误,但是我无法使该功能正常工作。 I am not sure what the problem is.
我不确定是什么问题。 I have tried several different things, but none of them work.
我尝试了几种不同的方法,但是它们都不起作用。
Any suggestions? 有什么建议么?
$(document).ready(function() { $('#priceSelect').change(function(){ if($(this).val() == 'FTLW' ){ $('#sourceBtns4').prop("checked", true); } }); });
You can't have two elements with the same id
. 您不能有两个具有相同
id
元素。 Your div
and your select
both had an id
of priceSelect
. 您的
div
和您的select
都具有一个priceSelect
的id
。 It's only the select
that you care about. 这只是您关心的
select
。
$(document).ready(function() { $('#priceSelect').change(function(){ console.log($(this).val()); if($(this).val() == 'FTLW' ){ $('#sourceBtns4').prop("checked", true); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <select name="priceSelect" id="priceSelect" class="form-control"> <option value="FTLW">Domestic Full Truck Load</option> <option value="PTLW">Domestic Partial Truck Load</option> <option value="FTL" selected>International</option> </select> </div> <div class="radio-inline" id="sourceDiv" role="group"> <input type="radio" id="sourceBtns1" name="sourceBtn" class="btn btn-lg" value="CN"><label for="sourceBtns1">CHINA</label> <input type="radio" id="sourceBtns2" name="sourceBtn" class="btn btn-lg" value="ID" ><label for="sourceBtns2">INDONESIA</label> <input type="radio" id="sourceBtns3" name="sourceBtn" class="btn btn-lg" value="TH" ><label for="sourceBtns3">THAILAND</label> <input type="radio" id="sourceBtns4" name="sourceBtn" class="btn btn-lg" value="US"><label for="sourceBtns4">UNITED STATES</label> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.