检查是否选择了选项

时间:2014-10-15 11:44:59

标签: javascript jquery html datatables

我正在为项目运行dataTables插件。

请你告诉我我做错了什么(这非常简单,并没有显示任何错误):

<select name="DataTables_Table_0_length" aria-controls="DataTables_Table_0" class="form-control input-sm">
    <option value="10">10</option>
    <option value="25">25</option>
    <option value="50">50</option>
    <option value="100">100</option>
</select>

基本上这就是我所生成的。因此,我想为页面上的所有表选择50和100值,并仅在选择了这两个结果时显示生成的表的页脚。

我尝试了以下内容(仅适用于值=&#34; 50&#34;):

var selected_result = $('select option:nth(2)');
var tfoot = $('tfoot');
tfoot.hide();

if (selected_result.is(':selected')) { tfoot.show(); }

谢谢

3 个答案:

答案 0 :(得分:0)

使用.change()然后检查是否选择显示/隐藏页脚。

$(document).ready(function() {
$("#tfoot").hide();
    $("select").change(function() {
        if ($("select option:nth(2)").is(":selected")) {
            $("#tfoot").show();
        } else {
            $("#tfoot").hide();
        }
    });
});

http://jsfiddle.net/rcLrf18j/

答案 1 :(得分:0)

&#13;
&#13;
		function showOptionValue() {
			var optionSelected = document.getElementById("select_test");
			var optionSelected_value = optionSelected.options[optionSelected.selectedIndex].value;
			
			alert(optionSelected_value);
		}
&#13;
<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<meta charset="utf-8" />
	</head>
	<body>
	
		<form action = "" method = "post">
			<select name = "select_test" id = "select_test" >
				<option value = "10">10</option>
				<option value = "25">25</option>
				<option value = "50">50</option>
				<option value = "100">100</option>
			</select>
			<input type = "button" value = "show option value" onclick = "showOptionValue()"/>
		</form>
	</body>
</html>
&#13;
&#13;
&#13;

此代码显示单击按钮时选项的当前值。我让你用这个修改函数来做你想做的事情(意思是,显示你的页脚)。

希望它有所帮助!

答案 2 :(得分:0)

据推测,默认情况下页脚是隐藏的。如果要在显示页脚时显示50或100,则:

$(document).ready(function() {
    $('select[name=DataTables_Table_0_length]').change(function() {

        if (this.value == '50' || this.value == '100') {
            $("#tfoot").show();

        } else {
            $("#tfoot").hide();
        }
    });
});

$(document).ready(function() {
    $('select[name=DataTables_Table_0_length]').change(function() {
        this.value == '50' || this.value == '100'? $("#tfoot").show() : $("#tfoot").hide();
    });
});

甚至:

$(document).ready(function() {
    $('select[name=DataTables_Table_0_length]').change(function() {
        $("#tfoot")[this.value == '50' || this.value == '100'? 'show' : 'hide']()
    });
});
相关问题