2016-04-17 3 views
1

を事前に選択せずに、私のような見て私のThymeleafページでリストダウンいくつかのドロップ持っている:私は、ページを表示するとタイムの葉のドロップダウンリスト

<select name="institution.serviceDept" th:field="*{serviceDept}"> 
     <option th:each="choice : ${serviceDeptList}" 
       th:value="${choice.serviceDeptId}" 
       th:attr="choiceinstitutionId=${choice.serviceDeptId}, institutioninstitutionId=*{serviceDept.serviceDeptId}, 
       showselected=(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:selected="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:readonly="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:text="${choice.name}"></option> 
    </select> 

を、リストの最初の値が選択され、実際には表示されます手動で選択されていない場合でも選択された値として提出されます。すでに選択されていない限り、デフォルトでは何も選択しないことを好みます。私は「th:selected」を削除しても差は出ません。

「選択してください」など、何も選択しなかったり、デフォルト値を設定する方法を教えてもらえますか?

答えて

0

は、このような空の選択オプションを追加してください:

<option disabled="disabled" selected value=" "> -- select an option -- </option> 

あなたは別のオプションを選択したら、戻ってそれを選択することはできませんので、それは、無効です。

+0

をし、これによりエラーが発生します。私はThymeleafの文書であなたの例を調べようとしましたが、参照を見つけることができませんでした。あなたは答えを拡大してください。 – sonoerin

+0

たぶん 'value =" "'を試してみてください。どのようなエラーが出ますか? –

+0

<オプションが無効になっています>選択された値th:each = "choice:$ {serviceDeptList}"とエラーが発生しました。 'org.xml.sax.SAXParseException: '='文字が続きます。 – sonoerin

0

他の人が私のために働いていないので、私の答えを投稿してください(あなたのマイレージは違います)。どうやら、ヌルやリスト内のダミーオブジェクトを使用することはできません。また、あらゆる種類の例外がスローされます。あなたは、このような別のブランク​​3210追加され行うことができます。

<select name="institution.serviceDept" th:field="*{serviceDept}"> 
    <option th:value="0" text="Please Select"></option> 
     <option th:each="choice : ${serviceDeptList}" 
       th:value="${choice.serviceDeptId}" 
       th:attr="choiceinstitutionId=${choice.serviceDeptId}, institutioninstitutionId=*{serviceDept.serviceDeptId}, 
       showselected=(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:selected="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:readonly="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})" 
      th:text="${choice.name}"></option> 
    </select> 

注ダミーオプションタグのthの欠如、と私は私のオプションタグに「オプション無効に選択した値を」追加th:value

+0

値を取得して「削除」したければ、オプションを取得するには、暗黙のうちに '