2011-12-16 59 views
0

我有這個輸入文本字段,最初會說答案1,然後如果你focus()文本將消失。如果你blur()文字會回來。jquery輸入複雜incrimenting混亂

<input type="button" id="btnAdd" value="Add Answer" /> 
     <div id="formanswer1" style="margin-bottom:4px;" class="clonedInput">Answer:<input type="text" id="formanswer1" value="Answer 1" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Answer 1':this.value;"/></div> 
</div> 


<script> 

$('#btnAdd').click(function() { 
    var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
    var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

    // create the new element via clone(), and manipulate it's ID using newNum value 
    var newElem = $('#formanswer' + num).clone().attr('id', 'formanswer' + newNum).attr("onblur","this.value=!this.value?'Answer "+newNum+"':this.value;"); 

    // manipulate the name/id values of the input inside the new element 
    newElem.children(':first').attr('id', 'formanswer' + newNum).attr('value','Answer '+newNum).attr('onblur',"this.value=!this.value?'Answer "+ newNum +"':this.value;"); 

    // insert the new element after the last "duplicatable" input field 
    $('#formanswer' + num).after(newElem); 

}); 

</script> 

我無法弄清楚如何改變onblur()的值。我不確定是否我的語法錯誤,但我不能弄清楚這一點。 !?

+1

爲什麼你再次問這個問題? http://stackoverflow.com/questions/8529986/jquery-input-complicated-incrementing-mess – 2011-12-16 06:10:54

回答

0

,而不是直接的js用這個的onblur

.attr( '的onblur',函數(){

THIS.VALUE = THIS.VALUE 「答案」 + newNum:THIS.VALUE; });

0

HTML:

<div> 
<input type="button" id="btnAdd" value="Add Answer" /> 
<div id="formanswer1" style="margin-bottom:4px;" class="clonedInput">Answer:<input type="text" id="formanswer1" value="Answer 1" /></div> 
</div> 

的jQuery:

$('input#formanswer1').on('click',function(){ 
     $(this).val(""); 
}); 


$('input#formanswer1').on('blur',function(){ 
    if($(this).val() === ""){ 
     $(this).val("Answer 1") 
    } 
}); 

檢查小提琴:http://jsfiddle.net/gSQ94/12/