1

私は2つの選択があります。ユーザーがドロップダウン選択なしでフォームを送信すると、メッセージがDIVに表示されます。ユーザーが値を選択したときにdivを非表示にする(消える?)方法はありますか?

脚本:

$(document).ready(function () {
$("#go").click(function () {

if (document.getElementById('sel').selectedIndex == 0)
$("#msg").html("Please select 1");


if (document.getElementById('sel2').selectedIndex == 0)
$("#msg2").html("Please select 2");

});
});

形:

<form id="form1">  
<div id="msg"></div>                  
<select id="sel">
<option value="">-- select --</option>
<option value="valor1">Valor 1</option>
<option value="valor2">Valor 2</option>
</select>

<div id="msg2"></div>
<select id="sel2">
<option value="">-- select --</option>
<option value="valor1">Valor 1</option>
<option value="valor2">Valor 2</option>
</select>

<input type="button" id="go" value="Go" />
</form>
4

3 に答える 3

2

jQueryのchangeイベントを利用してできること。これにより、select 要素などの特定の要素の変更を監視できます。私はあなたのためにフィドルを作りました。サンプル コードは次の場所にあります。

$("#go").click(function () {
    if (document.getElementById('sel').selectedIndex == 0) $("#msg").html("Please select 1");
    if (document.getElementById('sel2').selectedIndex == 0) $("#msg2").html("Please select 2");
});

$('select').change(function() {
    if (document.getElementById('sel').selectedIndex != 0) $("#msg").fadeOut();
    if (document.getElementById('sel2').selectedIndex != 0) $("#msg2").fadeOut();
});

これはあなたにとって良い出発点になるはずです。お役に立てれば!

于 2013-03-21T03:01:10.807 に答える
1

divを非表示にする最良の方法は、CSSプロパティの表示を使用してnoneに設定することです。jQueryでは、hideメソッドを使用して、またはcssメソッドを介して手動でこれを行うことができます。

$('#msg').hide();
$('#msg2').css('display','none');
于 2013-03-21T02:56:34.433 に答える
1
$("#go").click(function () {

if ($('#sel').val() == '')
    $("#msg").text("Please select 1");


if ($('#sel2').val() == '')
$("#msg2").text("Please select 2");

});
$('#sel').change(function(){$('#msg').hide()})
$('#sel2').change(function(){$('#msg2').hide()})

および動作中のjsfiddle:http://jsfiddle.net/RmYbH/2/

于 2013-03-21T03:06:11.450 に答える