2016-11-26 11 views

答えて

3

function alert_phrase(id){ 
 
    var value = document.getElementById(id).value; 
 
    alert(value); 
 
}
<div id="exp" style="background-color: white;"> 
 
    <p id="content-exp" style="color: red;">HELLO WORLD!</p> 
 
    <input name="phrase" Placheholder="Enter text here" id="phrase" /> 
 
    <button onclick='alert_phrase(phrase)'>Alert this sentence</button> 
 
</div>
はこれを試してみてください...

<button onclick='alert_phrase("phrase")'>Alert this sentence</button> 

注:文字列としてidを渡すがalert_phraseする

は、ここに私のコードです。

スニペット:

function alert_phrase(id){ 
 
    var value = document.getElementById(id).value; 
 
    alert(value); 
 
}
<div id="exp" style="background-color: white;"> 
 
    <p id="content-exp" style="color: red;">HELLO WORLD!</p> 
 
    <input name="phrase" Placheholder="Enter text here" id="phrase" /> 
 
    <button onclick='alert_phrase("phrase")'>Alert this sentence</button> 
 
</div>

+0

感謝を役に立てば幸い!それは私が助けることができてうれしい@ rforn – willy

+0

うまくいった。私はこれらの自分を逃したときには、それが荒いと思っています。 – rfornal

6

あなたのパラメータに引用符を忘れています。これは

function alert_phrase(id){ 
 
     var value = document.getElementById(id).value; 
 
     alert(value); 
 
}
<div id="exp" style="background-color: white;"> 
 
<p id="content-exp" style="color: red;">HELLO WORLD!</p> 
 
<input name="phrase" Placheholder="Enter text here" id="phrase" /> 
 
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>

関連する問題