我正在寻找一个执行此操作的jQuery插件。
例如:
<label><input type="checkbox" depends_on="foo=5" name="boo" ... /> Check </label>
<select name="foo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
所以只有在下面选择“5”选项时才会启用该复选框。
或
<select name="foo" depends_on="boo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
<label><input type="checkbox" name="boo" ... /> Check </label>
在这种情况下,只有在选中“boo”时才能启用选择。
我设法提出了类似的代码:
$("*[depends_on]").each(function(){
var source = $(this);
var dep = $(this).attr('depends_on');
var target = dep.split("=");
$("*[name='"+target[0]+"']:not(:hidden)").change(function(){
if($(this).attr('checked')) {
source.removeClass('disabled');
source.removeAttr('disabled');
}
else{
source.attr('disabled', 'disabled');
source.addClass('disabled');
}
}).change();
});
似乎适用于依赖无线电的选择,但我想为任何类型的输入或组合(或至少大部分)实现这一点,而不必为每种输入类型编写单独的代码。
无论如何,是否有人知道这样的插件?或者对我上面的代码有建议? :)
答案 0 :(得分:7)
在这里: - )
http://jsfiddle.net/balupton/cxcmf/
(function($){
/**
* jQuery.fn.dependsOn
* @version 1.0.1
* @date September 22, 2010
* @since 1.0.0, September 19, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license Attribution-ShareAlike 2.5 Generic {@link http://creativecommons.org/licenses/by-sa/2.5/
*/
$.fn.dependsOn = function(source,value){
var $target = $(this),
$source = $(source),
source = $source.attr('name')||$source.attr('id');
// Add Data
var dependsOnStatus = $target.data('dependsOnStatus')||{};
dependsOnStatus[source] = false;
$target.data('dependsOnStatus',dependsOnStatus);
// Add Event
$source.change(function(){
var pass = false;
var $source = $(this); // so $source won't be a group for radios
// Determine
if ( (value === null) || (typeof value === 'undefined') ) {
// We don't have a value
if ( $source.is(':checkbox,:radio') ) {
pass = $source.is(':selected:enabled,:checked:enabled');
}
else {
pass = Boolean($source.val());
}
}
else {
// We do have a value
if ( $source.is(':checkbox,:radio') ) {
pass = $source.is(':selected:enabled,:checked:enabled') && ($source.val() == value);
}
else {
pass = $source.val() == value;
}
}
// Update
var dependsOnStatus = $target.data('dependsOnStatus')||{};
dependsOnStatus[source] = pass;
$target.data('dependsOnStatus',dependsOnStatus);
// Determine
var passAll = true;
$.each(dependsOnStatus, function(i,v){
if ( !v ) {
passAll = false;
return false; // break
}
});
// console.log(dependsOnStatus);
// Adjust
if ( !passAll ) {
$target.attr('disabled','disabled').addClass('disabled');
}
else {
$target.removeAttr('disabled').removeClass('disabled');
}
}).trigger('change');
// Chain
return this;
};
})(jQuery);
举个例子:
<强>使用Javascript:强>
$(function(){
$('#foo').dependsOn('#boo').dependsOn('#moo','test')
.dependsOn(":radio[name=doo]",'true');
});
<强> HTML:强>
<div>
<select name="foo" id="foo" >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
<input type="text" name="moo" id="moo" />
<input type="checkbox" name="boo" id="boo" />
<input type="radio" name="doo" value="false" />
<input type="radio" name="doo" value="true" />
<br/>
Type test in the textbox and check the checkbox to enable the select.
<br/>
By <a href="http://www.balupton.com" target="_blank">Benjamin "balupton" Lupton</a>, for <a href="http://stackoverflow.com/q/3731586/130638" target="_blank">a StackOverflow Question</a>
</div>
答案 1 :(得分:1)
我想出了这段代码:
$("input[target]").change(function(){
var target = $("[name='"+$(this).attr("target")+"']");
if($(this).is(":checked")){
target.attr("oldvalue",target.val()).val($(this).val());
}else{
target.val(target.attr("oldvalue"));
}
});
$("select").change(function(){
$("[target='"+ $(this).attr("name") +"'][value='"+ $(this).val() +"']").attr('checked', true);
});
对于HTML格式:
<label><input type="checkbox" target="foo" value="15"/> Check </label>
<select name="foo">
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
所以基本上,你所要做的就是在输入上有一个“target”属性,它应该匹配你想要绑定到的下拉列表的“name”属性。设置输入的值也应该在选中时选择相应下拉列表的值。
答案 2 :(得分:1)
jQuery disable button (NOT Submit) until field validates + Validation Plugin
jQuery enable/disable show/hide button w/ SELECT options. Get remaining option values
jQuery disable SELECT options based on Radio selected (Need support for all browsers)
答案 3 :(得分:0)
dependjs定义HTML中的依赖项。
<select name="foo">
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
<label><input type="checkbox" depend=[enabled=>[name=foo] option[value='5']] /> Check </label>
依赖项在dependent元素中定义。