2017-05-02 6 views
-2

JavaScript를 사용하여 HTML 양식의 단추를 클릭 한 후 텍스트 상자를 지우고 그것에 집중하려고합니다. 나는 수색했으며 나는 다른 사람들이 보는 것을하고있다. 웬일인지, 그것은 단지 작동하지 않고있다. 내가 놓친 게 있니?html/javascript - 텍스트 상자를 지우고 onClick

<button type="button" value="ButtonTwo" onclick="clear();">CLEAR and focus</button> 

<script type="text/javascript"> 
function calculate(){ 
var price = document.getElementById("Enter_Price").value; 
var priceNum = Number(price); 
var discount = document.getElementById("Discount").value; 
var discountNum = Number(discount); 
if(priceNum <= 0){ 

    window.alert("Please enter a price that is greater than 0."); 
    document.getElementById('Enter_Price').focus(); 
    } 
else{ 
    if(discountNum < 0 || discountNum > 100){ 
    window.alert("Please enter a value that is between 0 and 100."); 
    document.getElementById('Discount').focus(); 
    } 
    else{ 

    var result = priceNum * (1-(discountNum/100)); 
    document.getElementById("Result").value = "$" + result.toFixed(2); 

    } 

} 

} 
function clear(){ 

document.getElementById("Enter_Price").value = ""; 
document.getElementById('Enter_Price').focus(); 

} 
</script> 

답변

0

변경 FUNC 이름이 트리거 버튼의

function clear1(){ 
 
    document.getElementById("Enter_Price").value = ""; 
 
    document.getElementById('Enter_Price').focus(); 
 
} 
 
    \t
<input id="Enter_Price" type="text" value="100"> 
 
<button onclick="clear1()">CLEAR and focus</button> 
 
    \t 
 
    \t 
 
    \t 
 

0

귀하의 오류입니다.

<button type="button" value="ButtonTwo" onclick="**clear();**">CLEAR and focus</button> 

그것은 다음과 같이해야합니다 :

<button type="button" value="ButtonTwo" onclick="clear1()">CLEAR and focus</button> 

나는 이벤트 리스너를 사용하려면.

0

document.getElementById("delete").addEventListener("click", clear); 
 
function clear(){ 
 

 
document.getElementById("input1").value = ""; 
 
document.getElementById("input1").focus(); 
 

 
}
<input id = "input1" type = "text"> 
 

 
<button id ="delete" type="button" value="ButtonTwo" >CLEAR and focus</button>

document.getElementById("delete").addEventListener("click", clear); 
의도 한대로 clear이 작동하지 '않는 이유에이 훌륭한 대답을 참조하십시오. Is “clear” a reserved word in Javascript?

DOM을 사용할 수 있거나 완료되면 버튼에 '클릭'이벤트를 첨부하고 싶습니다. 인라인 핸들러 생성을 자제합니다.

function initApplication() { 
 
    document.getElementById('hello').addEventListener('click', function() { 
 
    document.getElementById("Enter_Price").value = ""; 
 
    document.getElementById('Enter_Price').focus(); 
 
    }) 
 
} 
 

 
/* 
 
Wait for the document to "complete" loading, then initiate your application. 
 
*/ 
 
document.onreadystatechange = function() { 
 
    if (document.readyState === "complete") { 
 
    initApplication(); 
 
    } 
 
}
<input type='number' id='Enter_Price' /> 
 
<button id='hello'> 
 
    Click &amp; Clear 
 
</button>