我正在尝试在没有文本/空格时弹出窗口。它在Firefox,Chrome和Safari中运行良好。
请在我的JavaScript文件中查看以下代码 - :
function submitQuestion(URL,docId,errorMessage)
{
var question = $('textField').value;
if(!question.blank())
{
var submitForm = $("question-form");
submitForm.action = URL+"docId="+docId+"&question="+encodeURIComponent(question);
//alert(submitForm.action);
submitForm.submit();
}
else
{
alert(errorMessage);
return false;
}
}
以上功能在Firefox,Safari和Chrome中运行正常,因为当文本框中没有任何内容(即空/空白)时,它会转到else&提示errorMessage作为弹出窗口,但在IE中,它不会去to else& errorMessage pop-up永远不会来。
请查看我的forntend表格的以下代码 - :
<form method="POST" id="question-form" onsubmit="return submitQuestion('https://localhost/question-post!input.jspa?','ABC12','Enter your question!');">
<span class="fieldwrap">
<input type="text" placeholder="Ask customers about this document" value="" maxlength="255" autocomplete="off" class="questionInputField" id="textField">
</span>
<div class="clear"></div>
<div id="question-submit" class="widget-div clearfix">
<input type="submit" value="Submit question to the portal" class="questionSubmitFormButton">
</div>
</div>
</form>
在IE中发生的事情是,当我们没有提供任何文本字段时,它将占位符作为文本字段的值。当我们将文本字段保持为空或空白时,它将占位符作为文本字段值而不是给出弹出警报,它转到if循环,这不应该是一个案例。
答案 0 :(得分:0)
要使用jQuery访问textField的值,您应该使用val()而不是value。
var question = $('textField').val();
if (question != '') {
//
}
else {
//
}
以下是一个工作示例:
<!DOCTYPE html>
<head>
<title>EXAMPLE</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function submitQuestion(URL, docId, errorMessage) {
var question = $('#textField').val();
if (question.trim() != "") {
var submitForm = $("#question-form");
submitForm.attr("action", URL + "docId=" + docId + "&question=" + encodeURIComponent(question));
return true;
}
else {
alert(errorMessage);
return false;
}
}
</script>
</head>
<body>
<form method="POST" id="question-form" onsubmit="return submitQuestion('https://localhost/question-post!input.jspa?','ABC12','Enter your question!');">
<span class="fieldwrap">
<input type="text" placeholder="Ask customers about this document" value="" maxlength="255" autocomplete="off" class="questionInputField" id="textField">
</span>
<div class="clear"></div>
<div id="question-submit" class="widget-div clearfix">
<input type="submit" value="Submit question to the portal" class="questionSubmitFormButton">
</div>
</form>
</body>