2016-09-02 28 views
0

私の要件は、チェックボックスをクリックして保存/更新テキストボックスを無効にし、チェックボックスをオフにして保存/更新する必要があります。チェックの間にlatest_fileの値は1でなければならず、チェックしていない間はその値は0でなければなりません。 disableFileNameは、#latestFile#fileNameが定義されている質問の戻りでチェックした値がチェックされていない場合

function disableFileName() { 
    var latestFile = $("#latestFile").is(":checked"); 
    if (latestFile) { 
     $("#fileName").attr("disabled", "disabled"); 
    } else { 
     $("#fileName").removeAttr("disabled"); 
    } 
} 
<table> 
    <tr> 
     <td>Process the latest file from the feed location </td> 
     <td> 
      <s:checkbox property="latestFile" styleId="latestFile" value="1" onclick="disableFileName();" tabindex="5" /> 
     </td> 
    </tr> 
    <tr> 
     <td>File Name</td> 
     <td nowrap="true"> 
      <s:text property="fileName" styleClass="textbox" styleId="fileName" style="{width:150}" tabindex="6" /> 
     </td> 
    </tr> 
</table> 
+2

'prop'を使用してください。 – Tushar

+0

申し訳ありませんが、私はあなたに手を差し伸べました.... :( –

+0

'attr()'の代わりに 'prop()'メソッドを使い、 ' – Tushar

答えて

0

javascript期待される結果:ここに私が試した私のコードです。あなたはif$("#latestFile").val(1);を使用することができます#latestFilevalueを切り替えるには、else

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script> 
 
function disableFileName() { 
 
    
 
    var latestFile = $("#latestFile").is(":checked"); 
 
    if (latestFile) { 
 
     $("#latestFile").val(1); 
 
     $("#fileName").attr("disabled", "disabled"); 
 
    } else { 
 
     $("#latestFile").val(0); 
 
     $("#fileName").removeAttr("disabled"); 
 
    } 
 
    console.log($("#latestFile").val()); 
 
} 
 
</script> 
 
<table> 
 
    <tr> 
 
    <td>Process the latest file from the feed location</td> 
 
    <td> 
 
     <input type="checkbox" property="latestFile" id="latestFile" value="1" onclick="disableFileName();" tabindex="5" /> 
 
    </td> 
 
    </tr> 
 
    <tr> 
 
    <td>File Name</td> 
 
    <td nowrap="true"> 
 
     <input type="text" property="fileName" class="textbox" id="fileName" style="width:150" tabindex="6" /> 
 
    </td> 
 
    </tr> 
 
</table>

0

$("#latestFile").val(1);は、以下に示すように、チェックボックスを無効にする代わりにattrpropを使用してみてください:

$("#fileName").prop("disabled", "true"); 

HTML :

<table> 
    <tr> 
     <td>Process the latest file from the feed location </td> 
     <td> 
      <input type="checkbox" id="latestFile" value="1" onclick="disableFileName();" tabindex="5" /> 
     </td> 
    </tr> 
    <tr> 
     <td>File Name</td> 
     <td nowrap="true"> 
      <input type="textbox" name="fileName" styleClass="textbox" id="fileName" style="width:150" tabindex="6" /> 
     </td> 
    </tr> 
</table> 

Javascriptを:ここで

function disableFileName() { 
    var latestFile = $("#latestFile").is(":checked"); 
    if (latestFile) { 
     $("#fileName").prop("disabled", "true"); 
     $("#latestFile").attr("value", "1"); 
    } else { 
     $("#fileName").removeAttr("disabled"); 
     $("#latestFile").attr("value", "0"); 
    } 
} 

はデモです:JSFiddle Demo

+0

「javascript」の問題点'.attr()'、 '.removeAttr()'を使う質問ですか? "_ – guest271314

+0

_"チェックボックスを無効にするには、attrの代わりにpropを使用してください "_ .attrの使用" () '、' .removeAttr() 'は期待通りの結果を返します。http://stackoverflow.com/a/39286580/を参照してください。 '.prop()'はQuestionの 'javascript'と同じ結果を返します。問題は '#latestFile'であり、'#fileName'は 'html'で定義されていないようです。 'disableFileName'が呼び出される前に定義されているかどうかは不明です – guest271314

0

みんなあなたの偉大な応答のために、私の完全なコードは

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script> 
function saveUpdate(){ 
var latestFile=("#latestFile").is(":checked"); 
var fileName =document.getElementsByName("fileName")[0].value; 
    if(!latestFile){ 
         if(trim(file)==""){ 
          str+="FileName ,\n"; 
         } else if (!((file.toLowerCase().indexOf(".xml")>-1 || file.toLowerCase().indexOf(".txt")>-1 ||file.toLowerCase().indexOf(".xls")>-1|| file.toLowerCase().indexOf(".csv")>-1 
        || file.toLowerCase().indexOf(".tsv")>-1) && (file.toLowerCase().indexOf(".xlsx")==-1))){ 
          alert("Invalid fileformat"); 
          return false; 
         } 
        } 
        if(fileName == 0){ 
         str = str+"\n"+"Please enter file details"; 
        }else{ 
         if(ext[ext.length-1]!="csv" && ext[ext.length-1]!="txt"){ 
          str = str+"\n"+"File format should be in .csv or .txt"; 
         }     } 
} 

function disableFileName() { 
    var latestFile = $("#latestFile").is(":checked"); 
    if (latestFile) { 
     $("#fileName").attr("disabled", "disabled"); 
    } else { 
     $("#fileName").removeAttr("disabled"); 
    } 
} 
</script> 
<table> 
    <tr> 
    <td>Process the latest file from the feed location</td> 
    <td> 
     <input type="checkbox" property="latestFile" id="latestFile" value="1" onclick="disableFileName();" tabindex="5" /> 
    </td> 
    </tr> 
    <tr> 
    <td>File Name</td> 
    <td nowrap="true"> 
     <input type="text" property="fileName" class="textbox" id="fileName" style="width:150" tabindex="6" /> 
    </td> 
    </tr> 
</table> 
<table cellspacing="0" cellpadding="0" width="100%" > 
        <tr> 
         <td > 
          <s:submit property="save" value="Save" styleClass="button" onclick="return saveUpdate();" tabindex="8" disabled="<%=lxrInvRolDetInf.getAddRight() ? false : true%>"/> 

          <s:submit property="update" value="Update" styleClass="button" onclick="return saveUpdate();" tabindex="8" disabled="<%=lxrInvRolDetInf.getUpdateRight() ? false : true%>"/> 
         </td><td align="left" colspan="5"> 
          <s:submit property="reset" value="Reset" styleClass="button" tabindex="9" /> 

         </td> 
        </tr> 
        <tr><td colspan="5"> 
          <font class="errorText"> 
           <s:errors/> 
          </font> 
         </td></tr> 
       </table> 

私は、チェックボックスのテキストをクリックしてくださいですありがとうボックスが無効になっていますが、チェックをはずして保存/更新しようとするとテキストボックスが有効にならない

関連する問題