显示隐藏数据加载和复选/取消单选按钮
show hide data on load and check/uncheck of radio buttons
运行一个脚本,我需要检查复选框的值是否被选中,然后运行jQuery代码用于隐藏并根据特定的选中值进行显示。我无法修复它。
尝试使用此代码
$(".choosezoho").click(function() {
var checkValue = $("input[type='radio']").is(':checked');
alert(checkValue);
});
这给了我在哪个复选框上勾选
的值这是我的html<div class="selectbox_radio">
<blockquote>
<div title="128167215">Training</div>
<input type="hidden" name="customer_name" id="customer_name" value="12817215~Training">
</blockquote>
<input type="radio" class="choosezoho" name="usethisexisting" checked value="12817215">Use Existing
<input type="radio" class="choosezoho" name="usethisexisting" value="12817215">Create New
<br/>
<small>If You Check "Choose New" Please Choose a New Contact from below List</small>
<br/>
</div>
<div class="selectbox" style="display:none;">
<select name="customer_name" id="customer_name_select" class="selectItemsLists">
<option></option>
</select>
</div>
和我正在尝试的是:如果复选框是选择新的,它应该加载选择框并隐藏上面的,反之亦然,在其他情况下。
它还应该检查初始选择是什么,并基于jQuery代码中的触发器
尝试使用$(".choosezoho").change()
代替点击,然后使用$(this)
查看是否被选中
请查看下面的代码片段。
为了实现这一点,我已经改变了单选按钮的值来识别和显示/隐藏相应的
现有的和新的客户HTML被id包裹,所以我们可以相应地显示/隐藏。
$(".choosezoho").on('change',function() {
var checkValue = $('input[name=usethisexisting]:checked').val()
if(checkValue=="new"){
$("#existingCustomer").hide();
$("#newCustomer").show();
}else{
$("#existingCustomer").show();
$("#newCustomer").hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selectbox_radio">
<blockquote id="existingCustomer">
<div title="128167215">Training</div>
<input type="hidden" name="customer_name" id="customer_name" value="12817215~Training">
</blockquote>
<input type="radio" class="choosezoho" name="usethisexisting" checked value="existing">Use Existing
<input type="radio" class="choosezoho" name="usethisexisting" value="new">Create New
<br/>
<div id="newCustomer" style='display:none;'>
<small>If You Check "Choose New" Please Choose a New Contact from below List</small>
<br/>
</div>
<div class="selectbox" style="display:none;">
<select name="customer_name" id="customer_name_select" class="selectItemsLists">
<option></option>
</select>
</div>
</div>
$(".choosezoho").on('change',function() {
var checkValue = $('input[name=usethisexisting]:checked').val()
if(checkValue=="new"){
$("#existingCustomer").hide();
$("#newCustomer").show();
$('.selectbox').show();
}else{
$("#existingCustomer").show();
$("#newCustomer").hide();
$('.selectbox').hide();
}
});
<div class="selectbox_radio">
<blockquote id="existingCustomer">
<div title="128167215">Training</div>
<input type="hidden" name="customer_name" id="customer_name" value="12817215~Training">
</blockquote>
<input type="radio" class="choosezoho" name="usethisexisting" checked value="existing">Use Existing
<input type="radio" class="choosezoho" name="usethisexisting" value="new">Create New
<br/>
<div id="newCustomer" style='display:none;'>
<small>If You Check "Choose New" Please Choose a New Contact from below List</small>
<br/>
</div>
<div class="selectbox" style="display:none;">
<select name="customer_name" id="customer_name_select" class="selectItemsLists">
<option></option>
</select>
</div>
</div>
你应该给你的每个单选输入一个唯一的id,然后把它们放在一个div中。这样你就可以显示/隐藏整个div。
然后,您可以将更改事件绑定到Create New单选输入,并查看它是否被选中,如果是,您可以显示选择框并隐藏单选字段。
$("#createNewID").bind("change",function() {
if($(this).is(':checked')){
$('.selectbox').show();
$('#radioInput').hide();
}
参见JFiddle: https://jsfiddle.net/1h2xaL5x/
$(".choosezoho").on('change',function() {
var checkValue = $('input[name=usethisexisting]:checked').val()
if(checkValue=="new"){
$("#existingCustomer").hide();
$("#newCustomer").show();
$('.selectbox').show();
}else{
$("#existingCustomer").show();
$("#newCustomer").hide();
$('.selectbox').hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="selectbox_radio">
<blockquote id="existingCustomer">
<div title="128167215">Training</div>
<input type="hidden" name="customer_name" id="customer_name" value="12817215~Training">
</blockquote>
<input type="radio" class="choosezoho" name="usethisexisting" checked value="existing">Use Existing
<input type="radio" class="choosezoho" name="usethisexisting" value="new">Create New
<br/>
<div id="newCustomer" style='display:none;'>
<small>If You Check "Choose New" Please Choose a New Contact from below List</small>
<br/>
</div>
<div class="selectbox" style="display:none;">
<select name="customer_name" id="customer_name_select" class="selectItemsLists">
<option></option>
</select>
</div>
</div>
现在它被更新了
这个解决方案与以前的解决方案没有太大的不同,但是,它显示了一些小细节,对我来说似乎很有用。该开关允许在更多情况下广泛使用,并使事情更清晰。
我理解"输入类型文本"也必须在"选择新"选项中显示,在其他情况下出现列表。对吧?
html也有一些小的变化。
$(".choosezoho").bind("change",function() {
if($(this).is(':checked')){
switch( $(this).attr('id') ) {
case "createNew":
$('#nameSelect1').hide();
$('#newCustomerDiv').show();
break;
case "useExisting":
$('#nameSelect1').show();
$('#newCustomerDiv').hide();
break;
}
}
});
//setDefault
$('#useExisting').change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selectbox_radio" >
<blockquote>
<div title="128167215"
id="trainingID">
Training
</div>
<div id="newCustomerDiv">
<label for="customerName">New customer name:</label>
<input type="input"
id="customerName"
name="customer_name"
id="customer_name"
value="12817215~Training">
</div>
</blockquote>
<input type="radio"
id = "useExisting"
class="choosezoho"
name="usethisexisting"
checked value="12817215">
Use Existing
<input type="radio"
id="createNew"
class="choosezoho"
name="usethisexisting"
value="12817215">
Create New
<br/>
<small>If You Check "Choose New" Please Choose a New
Contact from below List</small>
<br/>
</div>
<div id="nameSelect1" class="selectbox" style="display:none;">
<select name="customer_name" id="customer_name_select"
class="selectItemsLists">
<option value="John Doe">John Doe</option>
<option value="Jane Doe">Jane Doe</option>
</select>
</div>
相关文章:
- JavaScript - 如果未选中复选框,则取消选中单选按钮
- 取消选择单选按钮
- 收听单选按钮取消选择事件而不知道按钮's上下文(骨干视图)
- 一次取消选中所有JQuery单选按钮集
- 单击时取消选择单选按钮
- 使用类取消选中Javascript中的单选按钮
- 如何检测单选按钮取消选择事件
- 如何通过单击单选按钮使其取消选中
- 单击其他单选按钮后取消选中单选按钮
- JavaScript:取消选择时设置单选按钮的背景(单选按钮组)
- 反应 - 无法取消选中单选按钮
- jQuery:使用 parent()、next() 单击单选按钮上的样式和取消样式
- 用于清除/取消选择单选按钮列表中的项目的 Javascript
- 安卓单选按钮不可见或取消选中
- 角度单选按钮在 ng值更改时取消选择
- 复选框作为单选按钮,并在选中时添加删除值 取消选中
- 如何一次只选择一个单选按钮,并在数据来自数据库时在javascript中取消选择其他单选按钮
- 单击功能时取消选中其他单选按钮
- 启用/禁用文本框点击/取消单选按钮
- 显示隐藏数据加载和复选/取消单选按钮