2014-01-25 50 views
-1

我正在尝试获取用户在文本字段中输入的名称的首字母(大写字母)。我得到和错误,我的函数getInitials()没有定义。为什么我会得到这个错误?另外我想检查一下函数是否存在typeof。Uncaught ReferencEerror函数没有定义,如何使用typeof和if else?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<title>Second task HS</title> 
</head> 

<body> 

<form name="myForm" id="eForm" action="#"> 
<label for="name">Name</label> 
    <input type="text" id="name" name="fullname"/><br> 
    <input name="button" type="button" value="Pick" onclick="getInitials();"/> 

</form> 
<div id="result"> 
</div> 

<script type="javascript"> 

    var nameInput = document.getElementById('name').value;//I need to stringify  the input and use it! 
    var arr, nameArr, first, last; 
    nameArr = name.split(' '); 
    first = nameArr[0][0].toUpperCase(); 
    last = nameArr[nameArr.length - 1][0].toUpperCase(); 
     if(typeof getInitials == 'function'){ 
     function getInitials(nameArr) { 
      return {first: first, last: last}; 
      } 
       getInitials(nameInput); 
     }else{ 
      alert('Check getInitials!'); 
     } 

</script> 

</body> 

</html> 
+0

您是否试图从大写字母的用户名中获取名称? –

+0

是的,然后选择第一个字母。我不明白为什么函数未定义。 –

+0

请看看我的答案。 –

回答

0

您在<script type="javascript">错过了文字,语句应该是这样的<script type="text/javascript">

这里是你想要的简单版本,试试吧,这个程序需要名字或者名字和姓氏。

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<title>Second task HS</title> 

<script type="text/javascript"> 
var arr, first, last; 
function getInitials() { 
    var nameInput = document.getElementById('name').value; //I need to stringify 
    nameArr = nameInput.split(' '); 
    if(nameArr.length > 1){ 
     first = nameArr[0].toUpperCase(); 
     last = nameArr[1].toUpperCase(); 

    }else{ 
     first = nameInput.toUpperCase(); 
    } 

    var result = {first: first, last: last}; 
    alert(result.first); 
    alert(result.last); 
} 
</script> 
</head> 

<body> 
<label for="name">Name</label> 
<input type="text" id="name" name="fullname"/><br> 
<input name="button" type="button" value="Pick" onclick="getInitials()"/> 
<div id="result"> 
</div> 
</body> 
</html> 
+1

是的我只是修改它给我只有首字母= firstArr [0] [0] .toUpperCase(); last = nameArr [nameArr.length - 1] [0] .toUpperCase(); –

1

从我看到的,你正在检查函数是否存在...在创建它之前!

尝试,而这个JS代码:

function getInitials(nameInput) { 
    var nameArr = nameInput.split(' '); 
    return { 
     first: nameArr[0][0].toUpperCase(), 
     last: nameArr[nameArr.length - 1][0].toUpperCase() 
    }; 
} 

function getInitialsFromInput() { 
    var nameInput = document.getElementById('name').value; 
    if(getInitials instanceof Function){ //strictly speaking, useless because it is obviously a function 
     alert(getInitials(nameInput)); 
    }else{ 
     alert('Check getInitials!'); 
    } 
} 

getInitialsFromInput() ; 

(和使用 “getInitialsFromInput()” 中的onclick采集输入的值)

0

您的代码是没有意义的。 getInitials刚开始时没有分配,因此按钮内的内联点击处理程序将无法工作。

如果需要使用大写的第一字符和字母的输入值,你可以试试:

document.querySelector('button[value=Pick]').onclick = getInitials; 

    function getInitials(e) { 
    var value = document.querySelector('#name') 
        .value.split(/\s+/) 
        .map(first2Upper); 
    if (value[0].length){ 
     var ret = {first: value[0], 
        last: value[1], 
        initials: value[0][0] +(value[1] && value[1][0] || '')}; 
     document.querySelector('#result').innerHTML = 
      value.join(' ') + ' (initials: '+ret.initials+')'; 
     return ret; 
    } else { 
     alert('please enter a value'); 
    } 
} 

function first2Upper(str) { 
    return str.slice(0,1).toUpperCase() + str.slice(1); 
} 

这里的a mockup in jsFiddle

+0

错误,'name.split('')[0] [0]'将返回第一个数组项的第一个字符*(或者如果name是一个空字符串则返回undefined)。它与'name.split('')[0] .charAt(0)'相同。 – Pavlo

+0

@Pavlo好吧,我的坏,马虎的阅读。编辑答案 – KooiInc

+0

第一行和第三行现在是相同的。这是故意的吗? – Pavlo

相关问题