2016-12-24 12 views
0

私のコンボボックスの選択した値を "val"というIDを持つ入力値に渡したいとします。私のコンボボックスの選択された値を別の入力値に渡す

私はjqueryの

$(document).ready(function() { 
      $('#btn').click(function(e) { 
       $('#combobox').change(function() { 
        var selected = $('option:selected', $(this)).text(); 
        alert($('#val').val(selected)); 
       }); 

      }); 
     }); 

を使用して値を渡す。しかし、それは働いていない試してみました。助けてください。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<link rel="stylesheet" 
 
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script 
 
\t src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script 
 
\t src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 
\t <div class="container"> 
 
\t \t <h1>Form</h1> 
 
\t \t <form method="post" id="form" name="form"> 
 
\t \t \t <div class="form-group row"> 
 
\t \t \t \t <label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label> 
 
\t \t \t \t <div class="col-xs-5 selectContainer"> 
 
\t \t \t \t \t <select id="combobox" class="form-control"> 
 
\t \t \t \t \t \t <option value="1">1</option> 
 
\t \t \t \t \t \t <option value="2">2</option> 
 
\t \t \t \t \t </select> 
 
\t \t \t \t </div> 
 
\t \t \t \t <input id="val" type="hidden" name="val"> 
 
\t \t \t \t <input id="btn" class="btn btn-info" value="Submit"> 
 
\t \t \t </div> 
 
\t \t </form> 
 
\t </div> 
 

 
\t <script type=text/javascript> 
 
\t \t $(document).ready(function() { 
 
\t \t \t $('#btn').click(function(e) { 
 
\t \t \t \t $('#combobox').change(function() { 
 
\t \t \t \t \t var selected = $('option:selected', $(this)).text(); 
 
\t \t \t \t \t alert($('#val').val(selected)); 
 
\t \t \t \t }); 
 

 
\t \t \t }); 
 
\t \t }); 
 
\t </script> 
 
</body> 
 
</html>

+0

あなたのイベントハンドラを入れ子にしています。クリック内の変更。それは常にコードが間違っているという兆候です。 –

+0

ようこそスタックオーバーフロー。同様の回答が投稿されたときに最初の回答を授与するのが習慣であることに注意してください(後で最後に追加された回答ではありません)。 –

答えて

0

.change必要ありませんです。ボタンをクリックすると、ドロップダウンの選択した値に直接アクセスできます。以下のコードを確認してください。

\t \t $(document).ready(function() { 
 
\t \t \t $('#btn').click(function(e) { 
 
\t \t \t \t \t alert($('#combobox option:selected').val()); 
 
\t \t \t }); 
 
\t \t });
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<link rel="stylesheet" 
 
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script 
 
\t src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script 
 
\t src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 
\t <div class="container"> 
 
\t \t <h1>Form</h1> 
 
\t \t <form method="post" id="form" name="form"> 
 
\t \t \t <div class="form-group row"> 
 
\t \t \t \t <label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label> 
 
\t \t \t \t <div class="col-xs-5 selectContainer"> 
 
\t \t \t \t \t <select id="combobox" class="form-control"> 
 
\t \t \t \t \t \t <option value="1">1</option> 
 
\t \t \t \t \t \t <option value="2">2</option> 
 
\t \t \t \t \t </select> 
 
\t \t \t \t </div> 
 
\t \t \t \t <input id="val" type="hidden" name="val"> 
 
\t \t \t \t <input id="btn" class="btn btn-info" value="Submit"> 
 
\t \t \t </div> 
 
\t \t </form> 
 
\t </div> 
 
</body> 
 
</html>

0

あなたは、あなたのイベントハンドラを入れ子にしている#btn

$('#combobox').change(function() { 
    var selected = $('option:selected', $(this)).text(); 
    $('#val').val(selected) 
    alert($('#val').val()); 
}); 

https://jsfiddle.net/

0

にクリックイベントを削除する必要があります。クリック内の変更。それは常にコードが間違っているという兆候です。クリックごとに新しい変更ハンドラを追加します。これはあなたが望むものではありません。

あなたがやろうとしているものではない100%明らかですが、あなたは次のように起動することができます。

$(function() { 
    $('#combobox').change(function() { 
     var selected = $(this).val(); 
     $('#val').val(selected); 
     alert($('#val').val()); 
    }); 
}); 

注:あなたが使用する必要はありませんので、selectval()は、選択された値を返します。 option:selectedおよびそのtext

0

これを試してください。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<link rel="stylesheet" 
 
\t href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script 
 
\t src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script 
 
\t src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 
\t <div class="container"> 
 
\t \t <h1>Form</h1> 
 
\t \t <form method="post" id="form" name="form"> 
 
\t \t \t <div class="form-group row"> 
 
\t \t \t \t <label class="col-xs-3 control-label" style="margin-top: 10px;">Choice</label> 
 
\t \t \t \t <div class="col-xs-5 selectContainer"> 
 
\t \t \t \t \t <select id="combobox" class="form-control"> 
 
\t \t \t \t \t \t <option value="1">1</option> 
 
\t \t \t \t \t \t <option value="2">2</option> 
 
\t \t \t \t \t </select> 
 
\t \t \t \t </div> 
 
\t \t \t \t <input id="val" type="hidden" name="val"> 
 
\t \t \t \t <input id="btn" class="btn btn-info" value="Submit"> 
 
\t \t \t </div> 
 
\t \t </form> 
 
\t </div> 
 

 
\t <script type=text/javascript> 
 
\t \t $(document).ready(function() { 
 
\t \t \t 
 
\t \t \t \t $('#combobox').change(function() { 
 
\t \t \t \t \t var selected = $('option:selected', $(this)).text(); 
 
$('#val').val(selected) 
 
alert($('#val').val()); 
 
\t \t \t \t 
 

 
\t \t \t }); 
 
\t \t }); 
 
\t </script> 
 
</body> 
 
</html>

関連する問題