2017-10-24 5 views
1

このコードは動作しないリダイレクトが機能していません:テキストボックスが空であるJavaScript検証と

<form name="keeper" action=" 
       javascript:if(this.value='') 
       {alert('password empty');}; 
       else{location.href = window.document.keeper.page.value + '.php';}" 
    style="margin:2vh;"> 
    <div style="display:inline;"> 
     <input type="text" name="page"> 
     <input type="submit" value="Go"> 

     <noscript> 
      <div style="display:inline;color:#ff0000; background-color:#ffff66; 
    font:normal 11px tahoma,sans-serif;"> 
       <br> Javascript is required to access this<br>area. Yours seems to be disabled. 
      </div> 
     </noscript> 
    </div> 
</form> 

場合は、フォームの送信があったとき、私はalert("Password empty")にしたいです。それ以外の場合は、スクリプトに現在のページにwindow.document.keeper.page.value + '.php'をロードします。

+0

'(this.value = ')であればthis.value ==)場合でなければなりません''また、この方法は非常にまれであると推奨されません。このような何かを行ってください。 – Mouser

+0

@ Mouserそれは本当です。私はそれを試してみました。しかし、まだ動作していません。 – user8783930

答えて

0

に動作します==を再取得してもthis.value==null

をご確認ください。これを行うには、より多くの標準を守るより良い方法があります。 '('

document.querySelector("form[name=keeper]").addEventListener("submit", function(e){ 
 
    var input = document.querySelector("input[name=page]"); 
 
    //e refers to the event - submit 
 
    if (input.value == '') 
 
    { 
 
     alert("password empty"); 
 
    } 
 
    else 
 
    { 
 
    location.href = input.value + '.php'; 
 
    } 
 
    e.preventDefault(); //this will cancel the normal submit and execute the code above. 
 
    return false; 
 
})
<form name="keeper" style="margin:2vh;"> 
 
    <div style="display:inline;"> 
 
    <input type="text" name="page"> 
 
    <input type="submit" value="Go"> 
 

 
    <noscript> 
 
    
 
     <div style="display:inline;color:#ff0000; background-color:#ffff66; font:normal 11px tahoma,sans-serif;"> <br>Javascript is required to access this<br>area. Yours seems to be disabled.</div> 
 
    
 
    </noscript> 
 
    </div> 
 
</form>

+0

インラインJavaScriptを共有できますか? – user8783930

+0

このスクリプトブロックをフォームの下に追加します。 – Mouser

+0

ああ!それは働いている – user8783930

0

if conditionので、それはインラインactionブロックは非常に読み取り不能と推奨されていないことを

<form name="keeper" action="javascript:if(this.value=='' || this.value==null){alert('password empty');}else{location.href = window.document.keeper.page.value + '.php';}" style="margin:2vh;"> 
 
    <div style="display:inline;"> <input type="text" name="page"> <input type="submit" value="Go"> <noscript><div style="display:inline;color:#ff0000; background-color:#ffff66; font:normal 11px tahoma,sans-serif;"> <br>Javascript is required to access this<br>area. Yours seems to be disabled.</div></noscript></div> 
 
</form>

+0

この質問は半分です。ボックスが空の場合、アラートボックスが表示されます。しかし、ボックスが空でないときにも警告が表示されます。 – user8783930

+0

'if'の' this.page.value'はこれを修正します。 – Mouser