1

選択したアイテムに特定の単語が含まれているかどうかを確認したい。元:

  <select name='project_type' id='type'>
  <option value='1'>Interpretation - S</option>
  <option value='2'>Interpretation - K</option>
  <option value='5'>Editing</option>
  <option value='7'>Translation</option>
  </select>

Jクエリ:

<script type="text/javascript">
$(document).ready(function(){

        //Hide div w/id extra
       $("#work1").show();
       $("#work2").hide();
        // Add onclick handler to checkbox w/id selected
       $("#type").live('click', function(){

        // If checked
        if (!$("#type:selected").text(contains('interpretation')))
        {
            //show the hidden div
            $("#work1").show();
                    $("#work2").hide();
        }
        else    if ($("#type:selected").text(contains('interpretation')))
        {
            //otherwise, hide it
            $("#work2").show();
                    $("#work1").hide();
        }
      });

    });
</script>

テキストに「解釈」を含む選択された要素を取得する必要があります(その単語を含むすべての要素が必要です)。私はこれを作りました:$("#type:selected").text(contains('interpretation'))しかし、機能しません。お願いします、誰か助けてくれませんか?

4

4 に答える 4

2
$('#type').change(function() {
    if ($(this).find(':selected').text().indexOf('Interpretation') > -1) {
        alert('selected value contains interpretation');    
    }
});​

http://jsfiddle.net/HyG6D/

于 2012-09-20T08:21:41.603 に答える
2

試す:

index = $("#type:selected").text().indexOf('interpreatation');
if(index == -1)
    //Show
else
    //Hide

JavaScript には呼び出される関数がありませんcontains。既知の回避策は、特定のフレーズまたは単語のインデックスをチェックすることです。存在しない場合は -1 を返します。

編集:質問のコードから非表示/表示を変更しました。

于 2012-09-20T08:22:03.220 に答える
0

'type'はselectタグのIDであるため、セレクターは機能しません。:selected疑似セレクターは、selectタグではなく、optionタグ上にある必要があります。次のように、id='type'の選択タグで選択されたオプションタグを見つけます。

!$("#type option:selected").text(contains('interpretation')){ ...

参照: http ://api.jquery.com/selected-selector/

「:selectedセレクターは<option>要素に対して機能します...」

更新:あなたの処方が.contains 適切に使用されていないことに気づきませんでした。
.contains()は、あるDOM要素に別の要素が含まれているかどうかをテストします。したがって、代わりに、セレクターに関する上記のアドバイスとZerkimのソリューションを組み合わせて、次のようにします。

!$("#type option:selected").text().indexOf('Interpretation') > -1)

...あなたの状態のために。

于 2012-09-20T08:23:01.120 に答える
0

これは役立つものですか?

$("#type option").each(function(k,v){

    if($(v).text().indexOf('interpreatation') != -1){

       //do something or add element to an array


    }

});
于 2012-09-20T08:23:53.070 に答える